summaryrefslogtreecommitdiff
path: root/javax/swing/plaf/metal/MetalInternalFrameTitlePane.java
blob: 534f0ca34abc5d94fdc7b7c6559d90ff92a6faf1 (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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* MetalInternalFrameTitlePane.java
   Copyright (C) 2005 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package javax.swing.plaf.metal;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.Icon;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;


/**
 * The title pane for a {@link JInternalFrame} (see 
 * {@link MetalInternalFrameUI#createNorthPane(JInternalFrame)}).  This can 
 * be displayed in two styles: one for regular internal frames, and the other 
 * for "palette" style internal frames.
 */
public class MetalInternalFrameTitlePane extends BasicInternalFrameTitlePane 
{
 
  /**
   * A property change handler that listens for changes to the 
   * <code>JInternalFrame.isPalette</code> property and updates the title
   * pane as appropriate.
   */
  class MetalInternalFrameTitlePanePropertyChangeHandler
    extends PropertyChangeHandler
  {
    /**
     * Creates a new handler.
     */
    public MetalInternalFrameTitlePanePropertyChangeHandler()
    {
      super();
    }
    
    /**
     * Handles <code>JInternalFrame.isPalette</code> property changes, with all
     * other property changes being passed to the superclass.
     * 
     * @param e  the event.
     */
    public void propertyChange(PropertyChangeEvent e)
    {
      String propName = e.getPropertyName();
      if (propName.equals("JInternalFrame.isPalette"))
        {
          if (e.getNewValue().equals(Boolean.TRUE))
            setPalette(true);
          else
            setPalette(false);
        }
      else
        super.propertyChange(e);
    }
  }

  /**
   * A layout manager for the title pane.
   * 
   * @see #createLayout()
   */
  private class MetalTitlePaneLayout implements LayoutManager
  {
    /**
     * Creates a new <code>TitlePaneLayout</code> object.
     */
    public MetalTitlePaneLayout()
    {
      // Do nothing.
    }

    /**
     * Adds a Component to the Container.
     *
     * @param name The name to reference the added Component by.
     * @param c The Component to add.
     */
    public void addLayoutComponent(String name, Component c)
    {
      // Do nothing.
    }

    /**
     * This method is called to lay out the children of the Title Pane.
     *
     * @param c The Container to lay out.
     */
    public void layoutContainer(Container c)
    {

      Dimension size = c.getSize();
      Insets insets = c.getInsets();
      int width = size.width - insets.left - insets.right;
      int height = size.height - insets.top - insets.bottom;


      int loc = width - insets.right - 1;
      int top = insets.top + 2;
      int buttonHeight = height - 4;
      if (closeButton.isVisible())
        {
          int buttonWidth = closeIcon.getIconWidth();
          loc -= buttonWidth + 2;
          closeButton.setBounds(loc, top, buttonWidth, buttonHeight);
          loc -= 6;
        }

      if (maxButton.isVisible())
        {
          int buttonWidth = maxIcon.getIconWidth();
          loc -= buttonWidth + 4;
          maxButton.setBounds(loc, top, buttonWidth, buttonHeight);
        }

      if (iconButton.isVisible())
        {
          int buttonWidth = minIcon.getIconWidth();
          loc -= buttonWidth + 4;
          iconButton.setBounds(loc, top, buttonWidth, buttonHeight);
          loc -= 2;
        }

      Dimension titlePreferredSize = title.getPreferredSize();
      title.setBounds(insets.left + 5, insets.top, 
              Math.min(titlePreferredSize.width, loc - insets.left - 10), 
              height);

    }

    /**
     * This method returns the minimum size of the given Container given the
     * children that it has.
     *
     * @param c The Container to get a minimum size for.
     *
     * @return The minimum size of the Container.
     */
    public Dimension minimumLayoutSize(Container c)
    {
      return preferredLayoutSize(c);
    }

    /**
     * Returns the preferred size of the given Container taking
     * into account the children that it has.
     *
     * @param c The Container to lay out.
     *
     * @return The preferred size of the Container.
     */
    public Dimension preferredLayoutSize(Container c)
    {
      if (isPalette)
        return new Dimension(paletteTitleHeight, paletteTitleHeight);
      else
        return new Dimension(22, 22);
    }

    /**
     * Removes a Component from the Container.
     *
     * @param c The Component to remove.
     */
    public void removeLayoutComponent(Component c)
    {
      // Nothing to do here.
    }
  }

  /** A flag indicating whether the title pane uses the palette style. */
  protected boolean isPalette;
  
  /** 
   * The icon used for the close button - this is fetched from the look and
   * feel defaults using the key <code>InternalFrame.paletteCloseIcon</code>. 
   */
  protected Icon paletteCloseIcon;
  
  /**
   * The height of the title pane when <code>isPalette</code> is 
   * <code>true</code>.  This value is fetched from the look and feel defaults 
   * using the key <code>InternalFrame.paletteTitleHeight</code>.
   */
  protected int paletteTitleHeight;
   
  /** The label used to display the title for the internal frame. */
  JLabel title;
  
  /**
   * Creates a new title pane for the specified frame.
   * 
   * @param f  the internal frame.
   */
  public MetalInternalFrameTitlePane(JInternalFrame f)
  {
    super(f);
    isPalette = false;
  }
  
  /**
   * Fetches the colors used in the title pane.
   */
  protected void installDefaults()
  {
    super.installDefaults();
    selectedTextColor = MetalLookAndFeel.getControlTextColor();
    selectedTitleColor = MetalLookAndFeel.getWindowTitleBackground();
    notSelectedTextColor = MetalLookAndFeel.getInactiveControlTextColor();
    notSelectedTitleColor = MetalLookAndFeel.getWindowTitleInactiveBackground();
    
    paletteTitleHeight = UIManager.getInt("InternalFrame.paletteTitleHeight");
    paletteCloseIcon = UIManager.getIcon("InternalFrame.paletteCloseIcon");
    minIcon = MetalIconFactory.getInternalFrameAltMaximizeIcon(16);
    
    title = new JLabel(frame.getTitle(), 
            MetalIconFactory.getInternalFrameDefaultMenuIcon(), 
            SwingConstants.LEFT);
  }
  
  /**
   * Clears the colors used for the title pane.
   */
  protected void uninstallDefaults()
  {  
    super.uninstallDefaults();
    selectedTextColor = null;
    selectedTitleColor = null;
    notSelectedTextColor = null;
    notSelectedTitleColor = null;
    paletteCloseIcon = null;
    minIcon = null;
    title = null;
  }
  
  /**
   * Calls the super class to create the buttons, then calls
   * <code>setBorderPainted(false)</code> and 
   * <code>setContentAreaFilled(false)</code> for each button.
   */
  protected void createButtons()
  { 
    super.createButtons();
    closeButton.setBorderPainted(false);
    closeButton.setContentAreaFilled(false);
    iconButton.setBorderPainted(false);
    iconButton.setContentAreaFilled(false);
    maxButton.setBorderPainted(false);
    maxButton.setContentAreaFilled(false);
  }
  
  /**
   * Overridden to do nothing.
   */
  protected void addSystemMenuItems(JMenu systemMenu)
  {
    // do nothing
  }
  
  /**
   * Overridden to do nothing.
   */
  protected void showSystemMenu()
  {
      // do nothing    
  }
  
  /**
   * Adds the sub components of the title pane.
   */
  protected void addSubComponents()
  {
    // FIXME:  this method is probably overridden to only add the required 
    // buttons
    add(title);
    add(closeButton);
    add(iconButton);
    add(maxButton);
  }

  /**
   * Creates a new instance of <code>MetalTitlePaneLayout</code> (not part of
   * the public API).
   * 
   * @return A new instance of <code>MetalTitlePaneLayout</code>.
   */
  protected LayoutManager createLayout()
  {
    return new MetalTitlePaneLayout();
  }
  
  /**
   * Draws the title pane in the palette style.
   * 
   * @param g  the graphics device.
   * 
   * @see #paintComponent(Graphics)
   */
  public void paintPalette(Graphics g)
  {
    Color savedColor = g.getColor();
    Rectangle b = SwingUtilities.getLocalBounds(this);

    if (UIManager.get("InternalFrame.activeTitleGradient") != null
        && frame.isSelected())
      {
        MetalUtils.paintGradient(g, b.x, b.y, b.width, b.height,
                                 SwingConstants.VERTICAL,
                                 "InternalFrame.activeTitleGradient");
      }
    MetalUtils.fillMetalPattern(this, g, b.x + 4, b.y + 2, b.width 
            - paletteCloseIcon.getIconWidth() - 13, b.height - 5,
            MetalLookAndFeel.getPrimaryControlHighlight(), 
            MetalLookAndFeel.getBlack());
    
    // draw a line separating the title pane from the frame content
    Dimension d = getSize();
    g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
    g.drawLine(0, d.height - 1, d.width - 1, d.height - 1);
    
    g.setColor(savedColor);
  }

  /**
   * Paints a representation of the current state of the internal frame.
   * 
   * @param g  the graphics device.
   */
  public void paintComponent(Graphics g)
  {
    Color savedColor = g.getColor();
    if (isPalette)
      paintPalette(g);
    else
      {
        paintTitleBackground(g);
        paintChildren(g);
        Dimension d = getSize();
        if (frame.isSelected())
          g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
        else
          g.setColor(MetalLookAndFeel.getControlDarkShadow());
        
        // put a dot in each of the top corners
        g.drawLine(0, 0, 0, 0);
        g.drawLine(d.width - 1, 0, d.width - 1, 0);
        
        g.drawLine(0, d.height - 1, d.width - 1, d.height - 1);
        
        // draw the metal pattern
        if (UIManager.get("InternalFrame.activeTitleGradient") != null
            && frame.isSelected())
          {
            MetalUtils.paintGradient(g, 0, 0, getWidth(), getHeight(),
                                     SwingConstants.VERTICAL,
                                     "InternalFrame.activeTitleGradient");
          }

        Rectangle b = title.getBounds();
        int startX = b.x + b.width + 5;
        int endX = startX;
        if (iconButton.isVisible())
          endX = Math.max(iconButton.getX(), endX);
        else if (maxButton.isVisible()) 
          endX = Math.max(maxButton.getX(), endX);
        else if (closeButton.isVisible())
          endX = Math.max(closeButton.getX(), endX);
        endX -= 7;
        if (endX > startX)
          MetalUtils.fillMetalPattern(this, g, startX, 3, endX - startX, getHeight() - 6, Color.white, Color.gray);
      }
    g.setColor(savedColor);
  }
  
  /**
   * Sets the flag that controls whether the title pane is drawn in the 
   * palette style or the regular style.
   *  
   * @param b  the new value of the flag.
   */
  public void setPalette(boolean b)
  {
    isPalette = b;
    title.setVisible(!isPalette);
    iconButton.setVisible(!isPalette && frame.isIconifiable());
    maxButton.setVisible(!isPalette && frame.isMaximizable());
    if (isPalette)
      closeButton.setIcon(paletteCloseIcon);
    else
      closeButton.setIcon(closeIcon);
  }
  
  /**
   * Creates and returns a property change handler for the title pane.
   * 
   * @return The property change handler.
   */
  protected PropertyChangeListener createPropertyChangeListener()
  {
    return new MetalInternalFrameTitlePanePropertyChangeHandler();   
  }
}