001: package net.sourceforge.squirrel_sql.client.gui.db.aliasproperties;
002:
003: import net.sourceforge.squirrel_sql.client.IApplication;
004: import net.sourceforge.squirrel_sql.client.gui.db.aliasproperties.DriverPropertiesController;
005: import net.sourceforge.squirrel_sql.client.gui.db.SQLAlias;
006: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
007: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
008:
009: import javax.swing.*;
010: import java.awt.event.ActionEvent;
011: import java.awt.event.ActionListener;
012: import java.awt.event.KeyEvent;
013: import java.util.ArrayList;
014: import java.util.HashMap;
015: import java.util.Arrays;
016:
017: public class AliasPropertiesController {
018: private static HashMap<IIdentifier, AliasPropertiesController> _currentlyOpenInstancesByAliasID = new HashMap<IIdentifier, AliasPropertiesController>();
019:
020: private AliasPropertiesInternalFrame _frame;
021: private ArrayList<IAliasPropertiesPanelController> _iAliasPropertiesPanelControllers = new ArrayList<IAliasPropertiesPanelController>();
022: private IApplication _app;
023: private SQLAlias _alias;
024:
025: public static void showAliasProperties(IApplication app,
026: SQLAlias selectedAlias) {
027: AliasPropertiesController openProps = _currentlyOpenInstancesByAliasID
028: .get(selectedAlias.getIdentifier());
029: if (null == openProps) {
030: _currentlyOpenInstancesByAliasID.put(selectedAlias
031: .getIdentifier(), new AliasPropertiesController(
032: app, selectedAlias));
033: } else {
034: GUIUtils.moveToFront(openProps._frame);
035: }
036:
037: }
038:
039: private AliasPropertiesController(IApplication app,
040: SQLAlias selectedAlias) {
041: _app = app;
042: _alias = selectedAlias;
043: _frame = new AliasPropertiesInternalFrame(_alias.getName());
044:
045: _app.getMainFrame().addInternalFrame(_frame, false);
046:
047: GUIUtils.centerWithinDesktop(_frame);
048:
049: _frame.setVisible(true);
050:
051: _frame.btnOk.addActionListener(new ActionListener() {
052: public void actionPerformed(ActionEvent e) {
053: onOK();
054: }
055: });
056:
057: _frame.btnClose.addActionListener(new ActionListener() {
058: public void actionPerformed(ActionEvent e) {
059: onClose();
060: }
061:
062: });
063:
064: AbstractAction closeAction = new AbstractAction() {
065: private static final long serialVersionUID = 1L;
066:
067: public void actionPerformed(ActionEvent actionEvent) {
068: performClose();
069: }
070: };
071: KeyStroke escapeStroke = KeyStroke.getKeyStroke(
072: KeyEvent.VK_ESCAPE, 0);
073: _frame.getRootPane().getInputMap(
074: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
075: escapeStroke, "CloseAction");
076: _frame.getRootPane().getInputMap(
077: JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeStroke,
078: "CloseAction");
079: _frame.getRootPane().getInputMap(JComponent.WHEN_FOCUSED).put(
080: escapeStroke, "CloseAction");
081: _frame.getRootPane().getActionMap().put("CloseAction",
082: closeAction);
083:
084: loadTabs();
085:
086: }
087:
088: private void loadTabs() {
089: _iAliasPropertiesPanelControllers
090: .add(new SchemaPropertiesController(_alias, _app));
091: _iAliasPropertiesPanelControllers
092: .add(new DriverPropertiesController(_alias, _app));
093:
094: IAliasPropertiesPanelController[] pluginAliasPropertiesPanelControllers = _app
095: .getPluginManager().getAliasPropertiesPanelControllers(
096: _alias);
097:
098: _iAliasPropertiesPanelControllers.addAll(Arrays
099: .asList(pluginAliasPropertiesPanelControllers));
100:
101: for (int i = 0; i < _iAliasPropertiesPanelControllers.size(); i++) {
102: IAliasPropertiesPanelController aliasPropertiesController = _iAliasPropertiesPanelControllers
103: .get(i);
104:
105: int index = _frame.tabPane.getTabCount();
106: _frame.tabPane.add(aliasPropertiesController.getTitle(),
107: aliasPropertiesController.getPanelComponent());
108: _frame.tabPane.setToolTipTextAt(index,
109: aliasPropertiesController.getHint());
110:
111: }
112: }
113:
114: private void performClose() {
115: _currentlyOpenInstancesByAliasID.remove(_alias.getIdentifier());
116: _frame.dispose();
117: }
118:
119: private void onOK() {
120: for (int i = 0; i < _iAliasPropertiesPanelControllers.size(); i++) {
121: IAliasPropertiesPanelController aliasPropertiesController = _iAliasPropertiesPanelControllers
122: .get(i);
123: aliasPropertiesController.applyChanges();
124: }
125: performClose();
126: }
127:
128: private void onClose() {
129: performClose();
130: }
131:
132: }
|