001: package org.columba.core.gui.docking;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Component;
006: import java.awt.Graphics;
007: import java.awt.Graphics2D;
008: import java.awt.event.ActionEvent;
009: import java.beans.PropertyChangeEvent;
010: import java.beans.PropertyChangeListener;
011:
012: import javax.swing.AbstractAction;
013: import javax.swing.ImageIcon;
014: import javax.swing.JButton;
015: import javax.swing.JComponent;
016: import javax.swing.JPopupMenu;
017: import javax.swing.UIManager;
018:
019: import org.flexdock.docking.DockingManager;
020: import org.flexdock.docking.defaults.AbstractDockable;
021: import org.flexdock.util.DockingUtility;
022:
023: public class DockableView extends AbstractDockable implements
024: PropertyChangeListener {
025:
026: public static Color INACTIVE_LABEL_COLOR = UIManager
027: .getColor("Menu.foreground");
028:
029: public static Color ACTIVE_LABEL_COLOR = UIManager
030: .getColor("Menu.selectionForeground");
031:
032: private DockingPanel panel;
033:
034: private JComponent dragInitiator;
035:
036: private TitleBar titleBar;
037:
038: private JPopupMenu menu;
039:
040: protected JButton menuButton;
041:
042: public DockableView(String id, String title) {
043: super (id);
044:
045: panel = new DockingPanel(id, title);
046:
047: titleBar = panel.getTitleBar();
048:
049: menuButton = titleBar.addButton(new MenuIcon(), new MenuAction(
050: this .getPersistentId()), BorderLayout.WEST);
051: // titleBar.addButton(new PinIcon(),
052: // new PinAction(this.getPersistentId()), BorderLayout.EAST);
053: // titleBar.addButton(new MaximizeIcon(), new MaximizeAction(this
054: // .getPersistentId()), BorderLayout.EAST);
055: titleBar.addButton(new CloseIcon(), new CloseAction(this
056: .getPersistentId()), BorderLayout.EAST);
057:
058: dragInitiator = panel.getTitleBar();
059: setTabText(panel.getTitle());
060:
061: getDragSources().add(dragInitiator);
062: getFrameDragSources().add(dragInitiator);
063:
064: DockingManager.registerDockable(this );
065: DockableRegistry.getInstance().register(this );
066:
067: addPropertyChangeListener(this );
068: }
069:
070: public void setPopupMenu(final JPopupMenu menu) {
071: if (menu == null)
072: throw new IllegalArgumentException("menu == null");
073:
074: this .menu = menu;
075: }
076:
077: public boolean isActive() {
078: return getDockingProperties().isActive().booleanValue();
079: }
080:
081: public void setTitle(String title) {
082: TitleBar titleBar = (TitleBar) panel.getTitleBar();
083: titleBar.setTitle(title);
084: }
085:
086: public String getTitle() {
087: TitleBar titleBar = (TitleBar) panel.getTitleBar();
088: return titleBar.getTitle();
089: }
090:
091: public void setContentPane(JComponent c) {
092: this .panel.setContentPane(c);
093:
094: }
095:
096: public Component getComponent() {
097: return panel;
098: }
099:
100: class MenuAction extends AbstractAction {
101: String id;
102:
103: MenuAction(String id) {
104: this .id = id;
105:
106: putValue(AbstractAction.NAME, "m");
107: }
108:
109: public void actionPerformed(ActionEvent e) {
110: // FIXME: should we align the menu to the left instead?
111: // menu.show(b, b.getWidth() - menu.getWidth(), b.getHeight());
112: if (menu != null)
113: menu.show(menuButton, 0, menuButton.getHeight());
114:
115: // menu.setVisible(true);
116: }
117: }
118:
119: class CloseAction extends AbstractAction {
120: String id;
121:
122: CloseAction(String id) {
123: this .id = id;
124:
125: putValue(AbstractAction.NAME, "x");
126: }
127:
128: public void actionPerformed(ActionEvent e) {
129: DockingManager.close(DockableView.this );
130: }
131: }
132:
133: class MaximizeAction extends AbstractAction {
134: String id;
135:
136: MaximizeAction(String id) {
137: this .id = id;
138:
139: putValue(AbstractAction.NAME, "m");
140: }
141:
142: public void actionPerformed(ActionEvent e) {
143: // DockingManager.(DockableView.this);
144: }
145: }
146:
147: class PinAction extends AbstractAction {
148: String id;
149:
150: PinAction(String id) {
151: this .id = id;
152:
153: putValue(AbstractAction.NAME, "P");
154:
155: }
156:
157: public void actionPerformed(ActionEvent e) {
158: boolean minimize = DockingUtility
159: .isMinimized(DockableView.this ) ? false : true;
160: DockingManager.setMinimized(DockableView.this , minimize);
161:
162: }
163: }
164:
165: public void propertyChange(PropertyChangeEvent evt) {
166:
167: panel.setActive(isActive());
168:
169: }
170:
171: class CloseIcon extends ImageIcon {
172:
173: private CloseIcon() {
174: super ();
175: }
176:
177: public void paintIcon(Component c, Graphics g, int x, int y) {
178: Graphics2D g2 = (Graphics2D) g;
179:
180: int w = c.getWidth();
181: int h = c.getHeight();
182:
183: if (isActive())
184: g2.setColor(ACTIVE_LABEL_COLOR);
185: else
186: g2.setColor(INACTIVE_LABEL_COLOR);
187:
188: g2.drawLine(x + 2, y + 2, w - 4, h - 4);
189: g2.drawLine(w - 4, y + 2, x + 2, h - 4);
190:
191: }
192:
193: /**
194: * @see javax.swing.Icon#getIconHeight()
195: */
196: public int getIconHeight() {
197: return 6;
198: }
199:
200: /**
201: * @see javax.swing.Icon#getIconWidth()
202: */
203: public int getIconWidth() {
204: return 6;
205: }
206:
207: }
208:
209: class PinIcon extends ImageIcon {
210:
211: private PinIcon() {
212: super ();
213: }
214:
215: public void paintIcon(Component c, Graphics g, int x, int y) {
216: Graphics2D g2 = (Graphics2D) g;
217:
218: if (isActive())
219: g2.setColor(ACTIVE_LABEL_COLOR);
220: else
221: g2.setColor(INACTIVE_LABEL_COLOR);
222: g2.drawRect(3, 2, 4, 4);
223:
224: g2.drawLine(2, 2 + 4, 2 + 4 + 2, 2 + 4);
225: g2.drawLine(5, 6, 5, 8);
226:
227: }
228:
229: /**
230: * @see javax.swing.Icon#getIconHeight()
231: */
232: public int getIconHeight() {
233: return 8;
234: }
235:
236: /**
237: * @see javax.swing.Icon#getIconWidth()
238: */
239: public int getIconWidth() {
240: return 8;
241: }
242:
243: }
244:
245: class MaximizeIcon extends ImageIcon {
246:
247: private MaximizeIcon() {
248: super ();
249: }
250:
251: public void paintIcon(Component c, Graphics g, int x, int y) {
252: Graphics2D g2 = (Graphics2D) g;
253:
254: if (isActive())
255: g2.setColor(ACTIVE_LABEL_COLOR);
256: else
257: g2.setColor(INACTIVE_LABEL_COLOR);
258: g2.drawRect(2, 3, 5, 5);
259:
260: g2.drawLine(3 + 1, 1, 3 + 6, 1);
261: g2.drawLine(3 + 6, 1, 3 + 6, 1 + 6 - 1);
262:
263: }
264:
265: /**
266: * @see javax.swing.Icon#getIconHeight()
267: */
268: public int getIconHeight() {
269: return 8;
270: }
271:
272: /**
273: * @see javax.swing.Icon#getIconWidth()
274: */
275: public int getIconWidth() {
276: return 8;
277: }
278:
279: }
280:
281: class MenuIcon extends ImageIcon {
282:
283: private MenuIcon() {
284: super ();
285: }
286:
287: public void paintIcon(Component c, Graphics g, int x, int y) {
288: Graphics2D g2 = (Graphics2D) g;
289:
290: int[] xp = new int[3];
291: int[] yp = new int[3];
292:
293: int w = c.getWidth();
294: int h = c.getHeight();
295:
296: xp[0] = x + 1;
297: xp[1] = x + w - 3;
298: xp[2] = x + 1 + 4;
299:
300: yp[0] = y + 3;
301: yp[1] = y + 3;
302: yp[2] = y + 3 + (int) (h - 1) / 2 - 1;
303:
304: if (isActive())
305: g2.setColor(ACTIVE_LABEL_COLOR);
306: else
307: g2.setColor(INACTIVE_LABEL_COLOR);
308: g2.drawPolygon(xp, yp, 3);
309:
310: }
311:
312: /**
313: * @see javax.swing.Icon#getIconHeight()
314: */
315: public int getIconHeight() {
316: return 6;
317: }
318:
319: /**
320: * @see javax.swing.Icon#getIconWidth()
321: */
322: public int getIconWidth() {
323: return 10;
324: }
325:
326: }
327:
328: }
|