001: package de.ixdb.squirrel_sql.plugins.cache;
002:
003: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
004: import net.sourceforge.squirrel_sql.client.session.ISession;
005: import net.sourceforge.squirrel_sql.client.session.event.ISessionListener;
006: import net.sourceforge.squirrel_sql.client.session.event.SessionEvent;
007: import net.sourceforge.squirrel_sql.client.session.event.SessionAdapter;
008:
009: import javax.swing.*;
010: import javax.swing.table.DefaultTableModel;
011: import java.awt.event.MouseAdapter;
012: import java.awt.event.MouseEvent;
013: import java.util.prefs.Preferences;
014: import java.util.Vector;
015: import java.util.Arrays;
016:
017: public class NamespaceCtrl {
018: private NamespaceDlg _dlg;
019: private NamespaceCtrlListener _namespaceCtrlListener;
020: private ISession _session;
021:
022: private String[] _cols = new String[] { "Namespace",
023: "Associated aliases" };
024:
025: private static final String PREFS_KEY_ALIAS_NAME_TEMPLATE = "Squirrel.ixdb.aliasNameTemplate";
026: private static final String ALIAS_NAME_TEMPLATE_DEFAULT = "Cache %server %namespace";
027:
028: public NamespaceCtrl(ISession session,
029: String[][] nameSpacesAndAliases,
030: NamespaceCtrlListener namespaceCtrlListener) {
031: _session = session;
032:
033: _session.getApplication().getSessionManager()
034: .addSessionListener(new SessionAdapter() {
035: public void sessionClosed(SessionEvent evt) {
036: _dlg.setVisible(false);
037: _dlg.dispose();
038: }
039: });
040:
041: _namespaceCtrlListener = namespaceCtrlListener;
042: _dlg = new NamespaceDlg(session.getApplication().getMainFrame());
043:
044: DefaultTableModel dtm = new DefaultTableModel() {
045: public boolean isCellEditable(int row, int column) {
046: return false;
047: }
048: };
049:
050: dtm.setDataVector(nameSpacesAndAliases, _cols);
051:
052: _dlg.tblNamespaces.setModel(dtm);
053:
054: _dlg.tblNamespaces.addMouseListener(new MouseAdapter() {
055: public void mouseClicked(MouseEvent e) {
056: onMouseClicked(e);
057: }
058:
059: });
060:
061: String aliasNameTemplate = Preferences.userRoot().get(
062: PREFS_KEY_ALIAS_NAME_TEMPLATE,
063: ALIAS_NAME_TEMPLATE_DEFAULT);
064:
065: _dlg.txtAliasNameTemplate.setText(aliasNameTemplate);
066:
067: _dlg.setSize(400, 400);
068:
069: GUIUtils.centerWithinParent(_dlg);
070:
071: _dlg.setVisible(true);
072:
073: }
074:
075: private void onMouseClicked(MouseEvent e) {
076: if (1 < e.getClickCount()) {
077: int selRow = _dlg.tblNamespaces.getSelectedRow();
078:
079: if (-1 == selRow) {
080: return;
081: }
082:
083: DefaultTableModel dtm = (DefaultTableModel) _dlg.tblNamespaces
084: .getModel();
085:
086: Vector dataVector = dtm.getDataVector();
087:
088: Vector rowVector = (Vector) dataVector.get(selRow);
089: String[] selNamespaceAndAlias = (String[]) rowVector
090: .toArray(new String[rowVector.size()]);
091:
092: if (0 < selNamespaceAndAlias[1].length()) {
093: String msg = "The selected namespace already has one or more aliases.\n"
094: + "Do you want to create another alias for the selected namespace?.";
095:
096: if (JOptionPane.YES_OPTION != JOptionPane
097: .showConfirmDialog(_dlg, msg,
098: "Duplicate alias",
099: JOptionPane.YES_NO_CANCEL_OPTION)) {
100: return;
101: }
102: }
103:
104: String aliasNameTemplate = _dlg.txtAliasNameTemplate
105: .getText();
106:
107: if (null == aliasNameTemplate
108: || 0 == aliasNameTemplate.trim().length()) {
109: aliasNameTemplate = ALIAS_NAME_TEMPLATE_DEFAULT;
110: }
111:
112: Preferences.userRoot().put(PREFS_KEY_ALIAS_NAME_TEMPLATE,
113: aliasNameTemplate);
114:
115: String newAliasName = _namespaceCtrlListener
116: .nameSpaceSelected(_session,
117: selNamespaceAndAlias[0], aliasNameTemplate);
118:
119: if (0 == selNamespaceAndAlias[1].length()) {
120: selNamespaceAndAlias[1] = newAliasName;
121: } else {
122: selNamespaceAndAlias[1] += ";" + newAliasName;
123: }
124:
125: rowVector.remove(1);
126: rowVector.add(selNamespaceAndAlias[1]);
127:
128: Vector colsVector = new Vector(Arrays.asList(_cols));
129: dtm.setDataVector(dataVector, colsVector);
130:
131: _dlg.tblNamespaces.getSelectionModel()
132: .setSelectionInterval(selRow, selRow);
133: }
134: }
135:
136: }
|