001: package net.sourceforge.squirrel_sql.client.session.properties;
002:
003: /*
004: * Copyright (C) 2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.Component;
022: import java.awt.GridBagConstraints;
023: import java.awt.GridBagLayout;
024: import java.awt.Insets;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027:
028: import javax.swing.BorderFactory;
029: import javax.swing.JCheckBox;
030: import javax.swing.JLabel;
031: import javax.swing.JPanel;
032: import javax.swing.JScrollPane;
033: import javax.swing.JTextField;
034: import javax.swing.SwingConstants;
035: import javax.swing.event.ChangeEvent;
036: import javax.swing.event.ChangeListener;
037:
038: import net.sourceforge.squirrel_sql.client.IApplication;
039: import net.sourceforge.squirrel_sql.client.preferences.INewSessionPropertiesPanel;
040: import net.sourceforge.squirrel_sql.client.session.ISession;
041: import net.sourceforge.squirrel_sql.fw.gui.IntegerField;
042: import net.sourceforge.squirrel_sql.fw.gui.MultipleLineLabel;
043: import net.sourceforge.squirrel_sql.fw.util.StringManager;
044: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
045: import net.sourceforge.squirrel_sql.fw.util.StringUtilities;
046:
047: /**
048: * This panel allows the user to tailor object tree settings for a session.
049: *
050: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
051: */
052: public class SessionObjectTreePropertiesPanel implements
053: INewSessionPropertiesPanel, ISessionPropertiesPanel {
054:
055: private static final StringManager s_stringMgr = StringManagerFactory
056: .getStringManager(SessionObjectTreePropertiesPanel.class);
057:
058: private static boolean _objectTreeRefreshNeeded = false;
059:
060: /** Application API. */
061: private final IApplication _app;
062:
063: /** The actual GUI panel that allows user to do the maintenance. */
064: private final ObjectTreepropsPanel _myPanel;
065: private final JScrollPane _scrolledMyPanel;
066:
067: /** Session properties object being maintained. */
068: private SessionProperties _props;
069:
070: /**
071: * ctor specifying the Application API.
072: *
073: * @param app Application API.
074: *
075: * @throws IllegalArgumentException
076: * Thrown if <tt>null</tt> <tt>IApplication</tt>
077: * passed.
078: */
079: public SessionObjectTreePropertiesPanel(IApplication app)
080: throws IllegalArgumentException {
081: super ();
082: if (app == null) {
083: throw new IllegalArgumentException(
084: "Null IApplication passed");
085: }
086: _app = app;
087: _myPanel = new ObjectTreepropsPanel(app);
088: _scrolledMyPanel = new JScrollPane(_myPanel);
089: }
090:
091: /**
092: * Initialize this panel with the "New Session" settings.
093: *
094: * @param app Application API.
095: */
096: public void initialize(IApplication app) {
097: _props = _app.getSquirrelPreferences().getSessionProperties();
098: _myPanel.loadData(_props);
099: }
100:
101: public void initialize(IApplication app, ISession session)
102: throws IllegalArgumentException {
103: if (session == null) {
104: throw new IllegalArgumentException("Null ISession passed");
105: }
106: _props = session.getProperties();
107: _myPanel.loadData(_props);
108: }
109:
110: public Component getPanelComponent() {
111: return _scrolledMyPanel;
112: }
113:
114: public String getTitle() {
115: return ObjectTreepropsPanel.i18n.OBJECT_TREE;
116: }
117:
118: public String getHint() {
119: return ObjectTreepropsPanel.i18n.OBJECT_TREE;
120: }
121:
122: public void applyChanges() {
123: _myPanel.applyChanges(_props);
124: }
125:
126: /**
127: * @param objectTreeRefreshNeeded the objectTreeRefreshNeeded to set
128: */
129: public void setObjectTreeRefreshNeeded(
130: boolean objectTreeRefreshNeeded) {
131: _objectTreeRefreshNeeded = objectTreeRefreshNeeded;
132: }
133:
134: /**
135: * @return the objectTreeRefreshNeeded
136: */
137: public boolean isObjectTreeRefreshNeeded() {
138: return _objectTreeRefreshNeeded;
139: }
140:
141: private static final class ObjectTreepropsPanel extends JPanel {
142: /**
143: * This interface defines locale specific strings. This should be
144: * replaced with a property file.
145: */
146: interface i18n {
147: // i18n[sessionPropertiesPanel.catalogPrefix=Limit Catalog Objects using these comma-delimited prefixes:]
148: String CATALOG_PREFIX = s_stringMgr
149: .getString("sessionPropertiesPanel.catalogPrefix");
150: // i18n[sessionPropertiesPanel.limitRowsContents=Contents - Limit rows]
151: String LIMIT_ROWS_CONTENTS = s_stringMgr
152: .getString("sessionPropertiesPanel.limitRowsContents");
153: // i18n[sessionPropertiesPanel.schemaPrefix=Limit Schema Objects using these comma-delimited prefixes:]
154: String SCHEMA_PREFIX = s_stringMgr
155: .getString("sessionPropertiesPanel.schemaPrefix");
156: // i18n[sessionPropertiesPanel.showRowCount=Show Row Count for Tables (can slow application)]
157: String SHOW_ROW_COUNT = s_stringMgr
158: .getString("sessionPropertiesPanel.showRowCount");
159: // i18n[sessionPropertiesPanel.objectTree=Object Tree]
160: String OBJECT_TREE = s_stringMgr
161: .getString("sessionPropertiesPanel.objectTree");
162: }
163:
164: private IntegerField _contentsNbrRowsToShowField = new IntegerField(
165: 5);
166: private JCheckBox _contentsLimitRowsChk = new JCheckBox(
167: i18n.LIMIT_ROWS_CONTENTS);
168: private JCheckBox _showRowCountChk = new JCheckBox(
169: i18n.SHOW_ROW_COUNT);
170: private JTextField _schemaPrefixField = new JTextField(20);
171: private JTextField _catalogPrefixField = new JTextField(20);
172: private JTextField _objectFilterField = new JTextField(20);
173: // i18n[sessionPropertiesPanel.loadSchemasCatalogs=Load Schemas/Catalogs into object tree]
174: private JCheckBox _loadSchemasCatalogsChk = new JCheckBox(
175: s_stringMgr
176: .getString("sessionPropertiesPanel.loadSchemasCatalogs"));
177:
178: /**
179: * This object will update the status of the GUI controls as the user
180: * makes changes.
181: */
182: private final ControlMediator _controlMediator = new ControlMediator();
183:
184: ObjectTreepropsPanel(IApplication app) {
185: super ();
186: createGUI();
187: }
188:
189: void loadData(SessionProperties props) {
190: _contentsNbrRowsToShowField.setInt(props
191: .getContentsNbrRowsToShow());
192: _contentsLimitRowsChk.setSelected(props
193: .getContentsLimitRows());
194: _showRowCountChk.setSelected(props.getShowRowCount());
195: _schemaPrefixField.setText(props.getSchemaPrefixList());
196: _catalogPrefixField.setText(props.getCatalogPrefixList());
197: if (props.getObjectFilter() != null)
198: _objectFilterField.setText(props.getObjectFilter());
199: _loadSchemasCatalogsChk.setSelected(props
200: .getLoadSchemasCatalogs());
201:
202: updateControlStatus();
203: }
204:
205: void applyChanges(SessionProperties props) {
206: props.setContentsNbrRowsToShow(_contentsNbrRowsToShowField
207: .getInt());
208: props.setContentsLimitRows(_contentsLimitRowsChk
209: .isSelected());
210: props.setShowRowCount(_showRowCountChk.isSelected());
211: // detect whether or not the object tree needs refreshing by comparing
212: // old value to new and see if they changed.
213: final String oldSchemaPrefixList = props
214: .getSchemaPrefixList();
215: final String oldCatalogPrefixList = props
216: .getCatalogPrefixList();
217: final String oldObjectFilter = props.getObjectFilter();
218: final boolean oldLoadSchemasCatalogs = props
219: .getLoadSchemasCatalogs();
220: final String newSchemaPrefixList = _schemaPrefixField
221: .getText();
222: final String newCatalogPrefixList = _catalogPrefixField
223: .getText();
224: final String newObjectFilter = _objectFilterField.getText();
225: final boolean newLoadSchemasCatalogs = _loadSchemasCatalogsChk
226: .isSelected();
227: props.setSchemaPrefixList(newSchemaPrefixList);
228: props.setCatalogPrefixList(newCatalogPrefixList);
229: props.setObjectFilter(newObjectFilter);
230: props.setLoadSchemasCatalogs(newLoadSchemasCatalogs);
231: _objectTreeRefreshNeeded = false;
232: if (!StringUtilities.areStringsEqual(oldSchemaPrefixList,
233: newSchemaPrefixList)) {
234: _objectTreeRefreshNeeded = true;
235: }
236: if (!StringUtilities.areStringsEqual(oldCatalogPrefixList,
237: newCatalogPrefixList)) {
238: _objectTreeRefreshNeeded = true;
239: }
240: if (!StringUtilities.areStringsEqual(oldObjectFilter,
241: newObjectFilter)) {
242: _objectTreeRefreshNeeded = true;
243: }
244: if (oldLoadSchemasCatalogs != newLoadSchemasCatalogs) {
245: _objectTreeRefreshNeeded = true;
246: }
247: }
248:
249: private void updateControlStatus() {
250: _contentsNbrRowsToShowField
251: .setEnabled(_contentsLimitRowsChk.isSelected());
252: }
253:
254: private void createGUI() {
255: setLayout(new GridBagLayout());
256: final GridBagConstraints gbc = new GridBagConstraints();
257: gbc.anchor = GridBagConstraints.WEST;
258: gbc.fill = GridBagConstraints.HORIZONTAL;
259: gbc.insets = new Insets(4, 4, 4, 4);
260:
261: gbc.gridx = 0;
262: gbc.gridy = 0;
263: add(createObjectTreePanel(), gbc);
264:
265: ++gbc.gridy;
266: add(createFilterPanel(), gbc);
267: }
268:
269: private JPanel createObjectTreePanel() {
270: final JPanel pnl = new JPanel(new GridBagLayout());
271: pnl.setBorder(BorderFactory
272: .createTitledBorder(i18n.OBJECT_TREE));
273: final GridBagConstraints gbc = new GridBagConstraints();
274: gbc.fill = GridBagConstraints.HORIZONTAL;
275: gbc.insets = new Insets(4, 4, 4, 4);
276: gbc.anchor = GridBagConstraints.CENTER;
277:
278: _contentsLimitRowsChk.addChangeListener(_controlMediator);
279:
280: _contentsNbrRowsToShowField.setColumns(5);
281:
282: gbc.gridwidth = GridBagConstraints.REMAINDER;
283: gbc.gridx = 0;
284: gbc.gridy = 0;
285: pnl.add(_loadSchemasCatalogsChk, gbc);
286:
287: ++gbc.gridy; // new line
288: gbc.gridx = 0;
289: pnl.add(_showRowCountChk, gbc);
290:
291: ++gbc.gridy; // new line
292: gbc.gridx = 0;
293: gbc.gridwidth = 2;
294: pnl.add(_contentsLimitRowsChk, gbc);
295: gbc.gridwidth = 1;
296: gbc.gridx += 2;
297: pnl.add(_contentsNbrRowsToShowField, gbc);
298: ++gbc.gridx;
299: gbc.gridwidth = GridBagConstraints.REMAINDER;
300: // i18n[generalPropertiesPanel.rows=rows]
301: pnl.add(new JLabel(s_stringMgr
302: .getString("generalPropertiesPanel.rows")), gbc);
303:
304: return pnl;
305: }
306:
307: private JPanel createFilterPanel() {
308: final JPanel pnl = new JPanel(new GridBagLayout());
309: // i18n[sessionObjectTreePropetiesPanel.filters=Filters]
310: pnl
311: .setBorder(BorderFactory
312: .createTitledBorder(s_stringMgr
313: .getString("sessionObjectTreePropetiesPanel.filters")));
314:
315: final GridBagConstraints gbc = new GridBagConstraints();
316: gbc.fill = GridBagConstraints.HORIZONTAL;
317: gbc.anchor = GridBagConstraints.WEST;
318: gbc.insets = new Insets(4, 4, 4, 4);
319: gbc.weightx = 1.0;
320:
321: gbc.gridx = 0;
322: gbc.gridy = 0;
323: gbc.insets = new Insets(4, 4, 0, 4);
324: pnl
325: .add(new JLabel(i18n.SCHEMA_PREFIX,
326: SwingConstants.RIGHT), gbc);
327: ++gbc.gridy;
328:
329: // i18n[SessionObjectTreePropertiesPanel.schemaPrefixNote=(Note: This property only influences Schema display in the Object tree. To configure Schema loading use Alias properties.))
330: gbc.insets = new Insets(0, 4, 4, 4);
331: pnl
332: .add(
333: new MultipleLineLabel(
334: s_stringMgr
335: .getString("SessionObjectTreePropertiesPanel.schemaPrefixNote")),
336: gbc);
337: ++gbc.gridy;
338:
339: gbc.insets = new Insets(4, 4, 4, 4);
340: pnl.add(_schemaPrefixField, gbc);
341:
342: ++gbc.gridy;
343: pnl.add(new JLabel(i18n.CATALOG_PREFIX,
344: SwingConstants.RIGHT), gbc);
345: ++gbc.gridy;
346: pnl.add(_catalogPrefixField, gbc);
347: ++gbc.gridy;
348: // i18n[sessionPropertiesPanel.objectFilterMeans=Object Filter:\n"%" means match any substring of 0 or more characters\n"_" means match any one character]
349: pnl
350: .add(
351: new MultipleLineLabel(
352: s_stringMgr
353: .getString("sessionPropertiesPanel.objectFilterMeans")),
354: gbc);
355: ++gbc.gridy;
356: pnl.add(_objectFilterField, gbc);
357:
358: return pnl;
359: }
360:
361: /**
362: * This class will update the status of the GUI controls as the user
363: * makes changes.
364: */
365: private final class ControlMediator implements ChangeListener,
366: ActionListener {
367: public void stateChanged(ChangeEvent evt) {
368: updateControlStatus();
369: }
370:
371: public void actionPerformed(ActionEvent evt) {
372: updateControlStatus();
373: }
374: }
375: }
376: }
|