001: package net.sourceforge.squirrel_sql.plugins.syntax.netbeans;
002:
003: import org.netbeans.editor.DialogSupport;
004:
005: import javax.swing.*;
006: import javax.swing.border.EmptyBorder;
007: import java.awt.event.*;
008: import java.awt.*;
009: import java.util.Hashtable;
010:
011: import net.sourceforge.squirrel_sql.plugins.syntax.SyntaxPugin;
012: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
013: import net.sourceforge.squirrel_sql.fw.util.StringManager;
014: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
015: import net.sourceforge.squirrel_sql.client.session.ISession;
016:
017: /**
018: * Essentially a copy of DialogSupport.DefaultDialogFactory
019: */
020: public class SquirrelNBDialogFactory extends WindowAdapter implements
021: DialogSupport.DialogFactory, ActionListener {
022: private static final StringManager s_stringMgr = StringManagerFactory
023: .getStringManager(SquirrelNBDialogFactory.class);
024:
025: private JButton cancelButton;
026: private SyntaxPugin _plugin;
027: private boolean _findHintProvided;
028:
029: public SquirrelNBDialogFactory(SyntaxPugin plugin) {
030:
031: _plugin = plugin;
032: }
033:
034: /**
035: * Create a panel with buttons that will be placed according
036: * to the required alignment
037: */
038: JPanel createButtonPanel(JButton[] buttons, boolean sidebuttons) {
039: int count = buttons.length;
040:
041: JPanel outerPanel = new JPanel(new BorderLayout());
042: outerPanel.setBorder(new EmptyBorder(new Insets(sidebuttons ? 5
043: : 0, sidebuttons ? 0 : 5, 5, 5)));
044:
045: LayoutManager lm = new GridLayout(// GridLayout makes equal cells
046: sidebuttons ? count : 1, sidebuttons ? 1 : count, 5, 5);
047:
048: JPanel innerPanel = new JPanel(lm);
049:
050: for (int i = 0; i < count; i++)
051: innerPanel.add(buttons[i]);
052:
053: outerPanel.add(innerPanel, sidebuttons ? BorderLayout.NORTH
054: : BorderLayout.EAST);
055: return outerPanel;
056: }
057:
058: public Dialog createDialog(String title, JPanel panel,
059: boolean modal, JButton[] buttons, boolean sidebuttons,
060: int defaultIndex, int cancelIndex, ActionListener listener) {
061:
062: if (false == _findHintProvided
063: && "find".equalsIgnoreCase(title)) {
064: //ISession[] activeSessions = _plugin.getApplication().getSessionManager().getActiveSessions();
065: ISession[] activeSessions = new ISession[] { _plugin
066: .getApplication().getSessionManager()
067: .getActiveSession() };
068:
069: for (int i = 0; i < activeSessions.length; i++) {
070: //i18n[syntax.findExplain=Press F3 to go to next result. Press Ctrl+Shift+F7 to toggle highlight search.]
071: activeSessions[i].showMessage(s_stringMgr
072: .getString("syntax.findExplain"));
073: }
074: _findHintProvided = true;
075: }
076:
077: // create the dialog with given content
078: JDialog dlg = new JDialog(_plugin.getApplication()
079: .getMainFrame(), title, modal);
080: dlg
081: .setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
082: dlg.getContentPane().add(panel, BorderLayout.CENTER);
083:
084: // Add the buttons to it
085: JPanel buttonPanel = createButtonPanel(buttons, sidebuttons);
086: String buttonAlign = sidebuttons ? BorderLayout.EAST
087: : BorderLayout.SOUTH;
088: dlg.getContentPane().add(buttonPanel, buttonAlign);
089:
090: // add listener to buttons
091: if (listener != null) {
092: for (int i = 0; i < buttons.length; i++) {
093: ActionListener[] actionListeners = buttons[i]
094: .getActionListeners();
095:
096: boolean found = false;
097: for (int j = 0; j < actionListeners.length; j++) {
098: if (actionListeners[j].equals(listener)) {
099: // We don't add a listener to a button twice
100: // because the FindDialogSupport class will call
101: // this method with the same button instances
102: // whenever the replace dialog is opened.
103: //
104: // If we add the listener again and again replace
105: // will be done as often as the dialog is opened.
106: // This is especially nasty if the text to replace
107: // is part of the replacement text.
108: found = true;
109: }
110: }
111:
112: if (false == found) {
113: buttons[i].addActionListener(listener);
114: }
115: }
116: }
117:
118: // register the default button, if available
119: if (defaultIndex >= 0) {
120: dlg.getRootPane().setDefaultButton(buttons[defaultIndex]);
121: }
122:
123: // register the cancel button helpers, if available
124: if (cancelIndex >= 0) {
125: cancelButton = buttons[cancelIndex];
126: // redirect the Esc key to Cancel button
127: dlg.getRootPane()
128: .registerKeyboardAction(
129: this ,
130: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,
131: 0, true),
132: JComponent.WHEN_IN_FOCUSED_WINDOW);
133:
134: // listen on windowClosing and redirect it to Cancel button
135: dlg.addWindowListener(this );
136: }
137:
138: dlg.pack();
139:
140: GUIUtils.centerWithinParent(dlg);
141: return dlg;
142: }
143:
144: public void actionPerformed(ActionEvent evt) {
145: cancelButton.doClick(10);
146: }
147:
148: public void windowClosing(WindowEvent evt) {
149: cancelButton.doClick(10);
150: }
151: }
|