summaryrefslogtreecommitdiff
path: root/plugins/ui-designer-core/src/com/intellij/designer/palette/PalettePanel.java
blob: f81baca17551662c44b616f073f06a0ba421176f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.designer.palette;

import com.intellij.designer.PaletteToolWindowContent;
import com.intellij.designer.designSurface.DesignerEditorPanel;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.ui.ScrollPaneFactory;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceAdapter;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Alexander Lobas
 */
public class PalettePanel extends JPanel implements DataProvider, PaletteToolWindowContent {
  private final JPanel myPaletteContainer = new PaletteContainer();
  private List<PaletteGroupComponent> myGroupComponents = Collections.emptyList();
  private List<PaletteItemsComponent> myItemsComponents = Collections.emptyList();
  private List<PaletteGroup> myGroups = Collections.emptyList();
  private DesignerEditorPanel myDesigner;

  private final FocusListener myFocusListener = new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent e) {
      for (PaletteItemsComponent itemsComponent : myItemsComponents) {
        itemsComponent.clearSelection();
      }
    }
  };
  private final ListSelectionListener mySelectionListener = new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent event) {
      notifySelection(event);
    }
  };

  private final DragSourceListener myDragSourceListener = new DragSourceAdapter() {
    @Override
    public void dragDropEnd(DragSourceDropEvent event) {
      Component component = event.getDragSourceContext().getComponent();
      if (!event.getDropSuccess() &&
          component instanceof PaletteItemsComponent &&
          myDesigner != null &&
          myDesigner.getRootPane() == ((JComponent)component).getRootPane()) {
        myDesigner.getToolProvider().loadDefaultTool();
      }
    }
  };

  public PalettePanel() {
    super(new GridLayout(1, 1));

    JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myPaletteContainer);
    scrollPane.setBorder(null);
    add(scrollPane);

    new AnAction() {
      @Override
      public void actionPerformed(AnActionEvent e) {
        clearActiveItem();
      }
    }.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0)), scrollPane);

    if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
      DragSource.getDefaultDragSource().addDragSourceListener(myDragSourceListener);
    }
  }

  @Override
  public void dispose() {
    if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
      DragSource.getDefaultDragSource().removeDragSourceListener(myDragSourceListener);
    }
  }

  @Nullable
  public PaletteItem getActiveItem() {
    for (PaletteGroupComponent groupComponent : myGroupComponents) {
      if (groupComponent.isSelected()) {
        PaletteItem paletteItem = (PaletteItem)groupComponent.getItemsComponent().getSelectedValue();
        if (paletteItem != null) {
          return paletteItem;
        }
      }
    }
    return null;
  }

  @Override
  public void clearActiveItem() {
    if (getActiveItem() != null) {
      for (PaletteItemsComponent itemsComponent : myItemsComponents) {
        itemsComponent.clearSelection();
      }
      notifySelection(null);
    }
  }

  @Override
  public void refresh() {
    repaint();
  }

  public void loadPalette(@Nullable DesignerEditorPanel designer) {
    if (myDesigner == null && designer == null) {
      return;
    }
    if (myDesigner != null && designer != null && myGroups.equals(designer.getPaletteGroups())) {
      myDesigner = designer;
      restoreSelection();
      return;
    }

    for (PaletteItemsComponent itemsComponent : myItemsComponents) {
      itemsComponent.removeListSelectionListener(mySelectionListener);
    }

    myDesigner = designer;
    myPaletteContainer.removeAll();

    if (designer == null) {
      myGroups = Collections.emptyList();
      myGroupComponents = Collections.emptyList();
      myItemsComponents = Collections.emptyList();
    }
    else {
      myGroups = designer.getPaletteGroups();
      myGroupComponents = new ArrayList<PaletteGroupComponent>();
      myItemsComponents = new ArrayList<PaletteItemsComponent>();
    }

    for (PaletteGroup group : myGroups) {
      PaletteGroupComponent groupComponent = new PaletteGroupComponent(group);
      PaletteItemsComponent itemsComponent = new PaletteItemsComponent(group, designer);

      groupComponent.setItemsComponent(itemsComponent);
      groupComponent.addFocusListener(myFocusListener);
      myGroupComponents.add(groupComponent);

      itemsComponent.addListSelectionListener(mySelectionListener);
      myItemsComponents.add(itemsComponent);

      myPaletteContainer.add(groupComponent);
      myPaletteContainer.add(itemsComponent);
    }

    myPaletteContainer.revalidate();

    if (myDesigner != null) {
      restoreSelection();
    }
  }

  private void restoreSelection() {
    PaletteItem paletteItem = myDesigner.getActivePaletteItem();
    for (PaletteItemsComponent itemsComponent : myItemsComponents) {
      itemsComponent.restoreSelection(paletteItem);
    }
  }

  private void notifySelection(@Nullable ListSelectionEvent event) {
    if (event != null) {
      PaletteItemsComponent sourceItemsComponent = (PaletteItemsComponent)event.getSource();
      for (int i = event.getFirstIndex(); i <= event.getLastIndex(); i++) {
        if (sourceItemsComponent.isSelectedIndex(i)) {
          for (PaletteItemsComponent itemsComponent : myItemsComponents) {
            if (itemsComponent != sourceItemsComponent) {
              itemsComponent.clearSelection();
            }
          }
          PaletteItem paletteItem = (PaletteItem)sourceItemsComponent.getSelectedValue();
          if (paletteItem != null && !paletteItem.isEnabled()) {
            sourceItemsComponent.clearSelection();
          }
          break;
        }
      }
    }
    if (myDesigner != null) {
      myDesigner.activatePaletteItem(getActiveItem());
    }
  }

  @Override
  public Object getData(@NonNls String dataId) {
    if (PlatformDataKeys.FILE_EDITOR.is(dataId) && myDesigner != null) {
      return myDesigner.getEditor();
    }
    return null;
  }
}