mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noël Rouvignac
21.14.2015 70e742ae6cd5e85059a1b8efec00c46d5c54a7f1
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2008 Sun Microsystems, Inc.
 *      Portions Copyright 2014-2015 ForgeRock AS
 */
 
package org.opends.guitools.controlpanel.ui.components;
 
import static com.forgerock.opendj.util.OperatingSystem.isMacOS;
 
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
 
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.TreePath;
 
import org.opends.guitools.controlpanel.ui.renderer.TreeCellRenderer;
 
/**
 * The tree that is used in different places in the Control Panel (schema
 * browser, index browser or the LDAP entries browser).  It renders in a
 * different manner than the default tree (selection takes the whole width
 * of the tree, in a similar manner as happens with trees in Mac OS).
 *
 */
public class CustomTree extends JTree
{
  private static final long serialVersionUID = -8351107707374485555L;
  private Set<MouseListener> mouseListeners;
  private JPopupMenu popupMenu;
  private final int MAX_ICON_HEIGHT = 18;
 
  /**
   * Internal enumeration used to translate mouse events.
   *
   */
  private enum NewEventType
  {
    MOUSE_PRESSED, MOUSE_CLICKED, MOUSE_RELEASED
  }
 
  /** {@inheritDoc} */
  public void paintComponent(Graphics g)
  {
    int[] selectedRows = getSelectionRows();
    if (selectedRows == null)
    {
      selectedRows = new int[] {};
    }
    Insets insets = getInsets();
    int w = getWidth( )  - insets.left - insets.right;
    int h = getHeight( ) - insets.top  - insets.bottom;
    int x = insets.left;
    int y = insets.top;
    int nRows = getRowCount();
    for ( int i = 0; i < nRows; i++)
    {
      int rowHeight = getRowBounds( i ).height;
      if (isRowSelected(selectedRows, i))
      {
        g.setColor(TreeCellRenderer.selectionBackground);
      }
      else
      {
        g.setColor(TreeCellRenderer.nonselectionBackground);
      }
      g.fillRect( x, y, w, rowHeight );
      y += rowHeight;
    }
    final int remainder = insets.top + h - y;
    if ( remainder > 0 )
    {
      g.setColor(TreeCellRenderer.nonselectionBackground);
      g.fillRect(x, y, w, remainder);
    }
 
    boolean isOpaque = isOpaque();
    setOpaque(false);
    super.paintComponent(g);
    setOpaque(isOpaque);
  }
 
  private boolean isRowSelected(int[] selectedRows, int i)
  {
    for (int j=0; j<selectedRows.length; j++)
    {
      if (selectedRows[j] == i)
      {
        return true;
      }
    }
    return false;
  }
 
  /**
   * Sets a popup menu that will be displayed when the user clicks on the tree.
   * @param popMenu the popup menu.
   */
  public void setPopupMenu(JPopupMenu popMenu)
  {
    this.popupMenu = popMenu;
  }
 
  /** Default constructor. */
  public CustomTree()
  {
    putClientProperty("JTree.lineStyle", "Angled");
    // This mouse listener is used so that when the user clicks on a row,
    // the items are selected (is not required to click directly on the label).
    // This code tries to have a similar behavior as in Mac OS).
    MouseListener mouseListener = new MouseAdapter()
    {
      private boolean ignoreEvents;
      /** {@inheritDoc} */
      public void mousePressed(MouseEvent ev)
      {
        if (ignoreEvents)
        {
          return;
        }
        MouseEvent newEvent = getTranslatedEvent(ev);
 
        if (isMacOS() && ev.isPopupTrigger() &&
            ev.getButton() != MouseEvent.BUTTON1)
        {
          MouseEvent baseEvent = ev;
          if (newEvent != null)
          {
            baseEvent = newEvent;
          }
          int mods = baseEvent.getModifiersEx();
          mods &= InputEvent.ALT_DOWN_MASK | InputEvent.META_DOWN_MASK |
              InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK;
          mods |=  InputEvent.BUTTON1_DOWN_MASK;
          final MouseEvent  macEvent = new MouseEvent(
              baseEvent.getComponent(),
              baseEvent.getID(),
                System.currentTimeMillis(),
                mods,
                baseEvent.getX(),
                baseEvent.getY(),
                baseEvent.getClickCount(),
                false,
                MouseEvent.BUTTON1);
          // This is done to select the node when the user does a right
          // click on Mac OS.
          notifyNewEvent(macEvent, NewEventType.MOUSE_PRESSED);
        }
 
        if (ev.isPopupTrigger()
            && popupMenu != null
            && (getPathForLocation(ev.getPoint().x, ev.getPoint().y) != null
                || newEvent != null))
        {
          popupMenu.show(ev.getComponent(), ev.getX(), ev.getY());
        }
        if (newEvent != null)
        {
          notifyNewEvent(newEvent, NewEventType.MOUSE_PRESSED);
        }
      }
 
      /** {@inheritDoc} */
      public void mouseReleased(MouseEvent ev)
      {
        if (ignoreEvents)
        {
          return;
        }
        MouseEvent newEvent = getTranslatedEvent(ev);
        if (ev.isPopupTrigger()
            && popupMenu != null
            && !popupMenu.isVisible()
            && (getPathForLocation(ev.getPoint().x, ev.getPoint().y) != null
                || newEvent != null))
        {
          popupMenu.show(ev.getComponent(), ev.getX(), ev.getY());
        }
 
        if (newEvent != null)
        {
          notifyNewEvent(newEvent, NewEventType.MOUSE_RELEASED);
        }
      }
 
      /** {@inheritDoc} */
      public void mouseClicked(MouseEvent ev)
      {
        if (ignoreEvents)
        {
          return;
        }
        MouseEvent newEvent = getTranslatedEvent(ev);
        if (newEvent != null)
        {
          notifyNewEvent(newEvent, NewEventType.MOUSE_CLICKED);
        }
      }
 
      private void notifyNewEvent(MouseEvent newEvent, NewEventType type)
      {
        ignoreEvents = true;
        // New ArrayList to avoid concurrent modifications (the listeners
        // could be unregistering themselves).
        for (MouseListener mouseListener :
          new ArrayList<MouseListener>(mouseListeners))
        {
          if (mouseListener != this)
          {
            switch (type)
            {
            case MOUSE_RELEASED:
              mouseListener.mouseReleased(newEvent);
              break;
            case MOUSE_CLICKED:
              mouseListener.mouseClicked(newEvent);
              break;
            default:
              mouseListener.mousePressed(newEvent);
            }
          }
        }
        ignoreEvents = false;
      }
 
      private MouseEvent getTranslatedEvent(MouseEvent ev)
      {
        MouseEvent newEvent = null;
        int x = ev.getPoint().x;
        int y = ev.getPoint().y;
        if (getPathForLocation(x, y) == null)
        {
          TreePath path = getWidePathForLocation(x, y);
          if (path != null)
          {
            Rectangle r = getPathBounds(path);
            if (r != null)
            {
              int newX = r.x + r.width / 2;
              int newY = r.y + r.height / 2;
              // Simulate an event
              newEvent = new MouseEvent(
                  ev.getComponent(),
                  ev.getID(),
                  ev.getWhen(),
                  ev.getModifiersEx(),
                  newX,
                  newY,
                  ev.getClickCount(),
                  ev.isPopupTrigger(),
                  ev.getButton());
            }
          }
        }
        return newEvent;
      }
    };
    addMouseListener(mouseListener);
    if (getRowHeight() <= MAX_ICON_HEIGHT)
    {
      setRowHeight(MAX_ICON_HEIGHT + 1);
    }
  }
 
  /** {@inheritDoc} */
  public void addMouseListener(MouseListener mouseListener)
  {
    super.addMouseListener(mouseListener);
    if (mouseListeners == null)
    {
      mouseListeners = new HashSet<>();
    }
    mouseListeners.add(mouseListener);
  }
 
  /** {@inheritDoc} */
  public void removeMouseListener(MouseListener mouseListener)
  {
    super.removeMouseListener(mouseListener);
    mouseListeners.remove(mouseListener);
  }
 
  private TreePath getWidePathForLocation(int x, int y)
  {
    TreePath path = null;
    TreePath closestPath = getClosestPathForLocation(x, y);
    if (closestPath != null)
    {
      Rectangle pathBounds = getPathBounds(closestPath);
      if (pathBounds != null &&
         x >= pathBounds.x && x < getX() + getWidth() &&
         y >= pathBounds.y && y < pathBounds.y + pathBounds.height)
      {
        path = closestPath;
      }
    }
    return path;
  }
}