001: /*
002: * EditorTabSelectMenu.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.dbobjects;
013:
014: import java.awt.EventQueue;
015: import java.awt.Font;
016: import java.awt.event.ActionListener;
017: import javax.swing.JMenuItem;
018: import javax.swing.event.ChangeEvent;
019: import javax.swing.event.ChangeListener;
020: import workbench.gui.MainWindow;
021: import workbench.gui.components.WbMenu;
022: import workbench.gui.components.WbMenuItem;
023: import workbench.interfaces.FilenameChangeListener;
024: import workbench.log.LogMgr;
025: import workbench.resource.ResourceMgr;
026: import workbench.util.NumberStringCache;
027:
028: /**
029: * @author support@sql-workbench.net
030: */
031: public class EditorTabSelectMenu extends WbMenu implements
032: FilenameChangeListener, ChangeListener {
033: private MainWindow parentWindow;
034: private ActionListener target;
035: private String regularTooltip;
036: private String newTabTooltip;
037: public static final String PANEL_CMD_PREFIX = "panel_";
038:
039: public EditorTabSelectMenu(ActionListener l, String label,
040: String tooltipKeyNewTab, String tooltipKeyTab,
041: MainWindow parent) {
042: super (label);
043: parentWindow = parent;
044: target = l;
045: newTabTooltip = ResourceMgr.getDescription(tooltipKeyNewTab,
046: true);
047: regularTooltip = ResourceMgr
048: .getDescription(tooltipKeyTab, true);
049: if (parentWindow != null) {
050: updateMenu();
051: parentWindow.addFilenameChangeListener(this );
052: parentWindow.addIndexChangeListener(this );
053: }
054: }
055:
056: protected synchronized void updateMenu() {
057: if (parentWindow == null)
058: return;
059:
060: String[] panels = this .parentWindow.getPanelLabels();
061: if (panels == null)
062: return;
063:
064: int count = this .getItemCount();
065: // Make sure none of the items has an ActionListener attached
066: for (int i = 0; i < count; i++) {
067: JMenuItem item = this .getItem(i);
068: if (item != null && target != null) {
069: item.removeActionListener(target);
070: }
071: }
072:
073: this .removeAll();
074:
075: int current = this .parentWindow.getCurrentPanelIndex();
076:
077: JMenuItem show = new WbMenuItem(ResourceMgr
078: .getString("LblShowDataInNewTab"));
079: show.setActionCommand(PANEL_CMD_PREFIX + "-1");
080: show.setToolTipText(newTabTooltip);
081: show.addActionListener(target);
082: this .add(show);
083:
084: Font boldFont = show.getFont();
085: if (boldFont != null)
086: boldFont = boldFont.deriveFont(Font.BOLD);
087:
088: addSeparator();
089:
090: for (int i = 0; i < panels.length; i++) {
091: JMenuItem item = new WbMenuItem(panels[i]);
092: item.setActionCommand(EditorTabSelectMenu.PANEL_CMD_PREFIX
093: + NumberStringCache.getNumberString(i));
094: if (i == current && boldFont != null) {
095: item.setFont(boldFont);
096: }
097:
098: // The tooltip is the same for all items
099: item.setToolTipText(regularTooltip);
100: item.addActionListener(target);
101: this .add(item);
102: }
103: }
104:
105: /**
106: * This is a callback from the MainWindow if a tab has been
107: * renamed. As we are showing the tab names in the "Show table data"
108: * popup menu, we need to update the popup menu
109: */
110: public void fileNameChanged(Object sender, String newFilename) {
111: try {
112: updateMenu();
113: } catch (Exception e) {
114: LogMgr.logError("TableListPanel.fileNameChanged()",
115: "Error when updating the popup menu", e);
116: }
117: }
118:
119: public void stateChanged(ChangeEvent e) {
120: // Updating the menu needs to be posted because
121: // the ChangeEvent is also triggered when a tab has been
122: // removed (thus implicitely changing the index)
123: // but the changeEvent occurs before the actual
124: // panel is removed from the control.
125: EventQueue.invokeLater(new Runnable() {
126: public void run() {
127: updateMenu();
128: }
129: });
130: }
131: }
|