001: package net.sourceforge.squirrel_sql.plugins.laf;
002:
003: /*
004: * Copyright (C) 2001-2006 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; 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.Font;
023: import java.awt.GridBagConstraints;
024: import java.awt.GridBagLayout;
025: import java.awt.Insets;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028:
029: import javax.swing.BorderFactory;
030: import javax.swing.JButton;
031: import javax.swing.JCheckBox;
032: import javax.swing.JLabel;
033: import javax.swing.JPanel;
034:
035: import net.sourceforge.squirrel_sql.fw.gui.FontChooser;
036: import net.sourceforge.squirrel_sql.fw.gui.FontInfo;
037: import net.sourceforge.squirrel_sql.fw.gui.MultipleLineLabel;
038: import net.sourceforge.squirrel_sql.fw.gui.OutputLabel;
039: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
040: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
041: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
042: import net.sourceforge.squirrel_sql.fw.util.StringManager;
043:
044: import net.sourceforge.squirrel_sql.client.IApplication;
045: import net.sourceforge.squirrel_sql.client.preferences.IGlobalPreferencesPanel;
046:
047: /**
048: * The Fonts panel for the Global Preferences dialog.
049: *
050: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
051: */
052: class LAFFontsTab implements IGlobalPreferencesPanel {
053: private static final StringManager s_stringMgr = StringManagerFactory
054: .getStringManager(LAFFontsTab.class);
055:
056: /** Logger for this class. */
057: private static final ILogger s_log = LoggerController
058: .createLogger(LAFFontsTab.class);
059:
060: /** The plugin. */
061: private LAFPlugin _plugin;
062:
063: /** Plugin preferences object. */
064: //private LAFPreferences _prefs;
065: /** Look and Feel register. */
066: private LAFRegister _lafRegister;
067:
068: /** Fonts panel to display in the Global preferences dialog. */
069: private FontSelectionPanel _myPanel;
070:
071: /** Application API. */
072: //private IApplication _app;
073: /**
074: * Ctor.
075: *
076: * @param plugin The LAF plugin.
077: * @param lafRegister Look and Feel register.
078: *
079: * @throws IllegalArgumentException
080: * if <TT>LAFPlugin</TT>, or <TT>LAFRegister</TT> is <TT>null</TT>.
081: */
082: public LAFFontsTab(LAFPlugin plugin, LAFRegister lafRegister) {
083: super ();
084: if (plugin == null) {
085: throw new IllegalArgumentException("Null LAFPlugin passed");
086: }
087: if (lafRegister == null) {
088: throw new IllegalArgumentException(
089: "Null LAFRegister passed");
090: }
091: _plugin = plugin;
092: //_prefs = plugin.getLAFPreferences();
093: _lafRegister = lafRegister;
094: }
095:
096: /**
097: * Load panel with data from plugin preferences.
098: *
099: * @param app Application API.
100: *
101: * @throws IllegalArgumentException
102: * if <TT>IApplication</TT> is <TT>null</TT>.
103: */
104: public void initialize(IApplication app) {
105: if (app == null) {
106: throw new IllegalArgumentException(
107: "Null IApplication passed");
108: }
109: //_app = app;
110: ((FontSelectionPanel) getPanelComponent()).loadData();
111: }
112:
113: public void uninitialize(IApplication app) {
114: }
115:
116: /**
117: * Return the component to be displayed in the Preferences dialog.
118: *
119: * @return the component to be displayed in the Preferences dialog.
120: */
121: public synchronized Component getPanelComponent() {
122: if (_myPanel == null) {
123: _myPanel = new FontSelectionPanel(_plugin, _lafRegister);
124: }
125: return _myPanel;
126: }
127:
128: /**
129: * User has pressed OK or Apply in the dialog so save data from
130: * panel.
131: */
132: public void applyChanges() {
133: _myPanel.applyChanges();
134: }
135:
136: /**
137: * Return the title for this panel.
138: *
139: * @return the title for this panel.
140: */
141: public String getTitle() {
142: return FontSelectionPanel.i18n.TAB_TITLE;
143: }
144:
145: /**
146: * Return the hint for this panel.
147: *
148: * @return the hint for this panel.
149: */
150: public String getHint() {
151: return FontSelectionPanel.i18n.TAB_HINT;
152: }
153:
154: /**
155: * "Fonts" panel to be displayed in the preferences dialog.
156: */
157: private static final class FontSelectionPanel extends JPanel {
158: /**
159: * This interface defines locale specific strings. This should be
160: * replaced with a property file.
161: */
162: interface i18n {
163: // i18n[laf.warning=Note: Changes may not take effect until the application is restarted.]
164: String LAF_WARNING = s_stringMgr.getString("laf.warning");
165: // i18n[laf.tabTitle=Fonts]
166: String TAB_TITLE = s_stringMgr.getString("laf.tabTitle");
167: // i18n[laf.tabHint=Fonts]
168: String TAB_HINT = s_stringMgr.getString("laf.tabHint");
169: }
170:
171: /** Button to select font for menus. */
172: private FontButton _menuFontBtn;
173:
174: /** Button to select font for static text. */
175: private FontButton _staticFontBtn;
176:
177: /** Button to select font for status bars. */
178: private FontButton _statusBarFontBtn;
179:
180: /** Button to select font for other controls. */
181: private FontButton _otherFontBtn;
182:
183: private JLabel _menuFontLbl = new OutputLabel(" ");
184: private JLabel _staticFontLbl = new OutputLabel(" ");
185: private JLabel _statusBarFontLbl = new OutputLabel(" ");
186: private JLabel _otherFontLbl = new OutputLabel(" ");
187:
188: // i18n[laf.menuFontEanbled=Enabled]
189: private JCheckBox _menuFontEnabledChk = new JCheckBox(
190: s_stringMgr.getString("laf.menuFontEanbled"));
191: // i18n[laf.staticFontEanbled=Enabled]
192: private JCheckBox _staticFontEnabledChk = new JCheckBox(
193: s_stringMgr.getString("laf.staticFontEanbled"));
194: // i18n[laf.statusFontEanbled=Enabled]
195: private JCheckBox _statusBarFontEnabledChk = new JCheckBox(
196: s_stringMgr.getString("laf.statusFontEanbled"));
197: // i18n[laf.otherFontEanbled=Enabled]
198: private JCheckBox _otherFontEnabledChk = new JCheckBox(
199: s_stringMgr.getString("laf.otherFontEanbled"));
200:
201: private LAFPlugin _plugin;
202: private LAFRegister _lafRegister;
203:
204: private LAFPreferences _prefs;
205:
206: FontSelectionPanel(LAFPlugin plugin, LAFRegister lafRegister) {
207: super ();
208: _plugin = plugin;
209: _lafRegister = lafRegister;
210: _prefs = _plugin.getLAFPreferences();
211: createUserInterface();
212: }
213:
214: void loadData() {
215: _menuFontEnabledChk.setSelected(_prefs.isMenuFontEnabled());
216: _staticFontEnabledChk.setSelected(_prefs
217: .isStaticFontEnabled());
218: _statusBarFontEnabledChk.setSelected(_prefs
219: .isStatusBarFontEnabled());
220: _otherFontEnabledChk.setSelected(_prefs
221: .isOtherFontEnabled());
222:
223: FontInfo fi = _prefs.getMenuFontInfo();
224: _menuFontLbl.setText(fi != null ? fi.toString() : "");
225: fi = _prefs.getStaticFontInfo();
226: _staticFontLbl.setText(fi != null ? fi.toString() : "");
227: fi = _prefs.getStatusBarFontInfo();
228: _statusBarFontLbl.setText(fi != null ? fi.toString() : "");
229: fi = _prefs.getOtherFontInfo();
230: _otherFontLbl.setText(fi != null ? fi.toString() : "");
231:
232: _menuFontBtn.setEnabled(_prefs.isMenuFontEnabled());
233: _staticFontBtn.setEnabled(_prefs.isStaticFontEnabled());
234: _statusBarFontBtn.setEnabled(_prefs
235: .isStatusBarFontEnabled());
236: _otherFontBtn.setEnabled(_prefs.isOtherFontEnabled());
237: }
238:
239: void applyChanges() {
240: _prefs.setMenuFontInfo(_menuFontBtn.getFontInfo());
241: _prefs.setStaticFontInfo(_staticFontBtn.getFontInfo());
242: _prefs
243: .setStatusBarFontInfo(_statusBarFontBtn
244: .getFontInfo());
245: _prefs.setOtherFontInfo(_otherFontBtn.getFontInfo());
246:
247: _prefs.setMenuFontEnabled(_menuFontEnabledChk.isSelected());
248: _prefs.setStaticFontEnabled(_staticFontEnabledChk
249: .isSelected());
250: _prefs.setStatusBarFontEnabled(_statusBarFontEnabledChk
251: .isSelected());
252: _prefs.setOtherFontEnabled(_otherFontEnabledChk
253: .isSelected());
254:
255: try {
256: _lafRegister.updateApplicationFonts();
257: //_lafRegister.updateAllFrames();
258:
259: } catch (Exception ex) {
260: s_log.error("Error updating fonts", ex);
261: }
262: }
263:
264: private void createUserInterface() {
265: setLayout(new GridBagLayout());
266: final GridBagConstraints gbc = new GridBagConstraints();
267: gbc.anchor = GridBagConstraints.WEST;
268: gbc.fill = GridBagConstraints.HORIZONTAL;
269: gbc.insets = new Insets(4, 4, 4, 4);
270: gbc.gridx = 0;
271: gbc.gridy = 0;
272: add(createFontsPanel(), gbc);
273:
274: ++gbc.gridy;
275: gbc.gridwidth = GridBagConstraints.REMAINDER;
276: add(new MultipleLineLabel(i18n.LAF_WARNING), gbc);
277: }
278:
279: private JPanel createFontsPanel() {
280: _menuFontBtn =
281: // i18n[laf.menus=Menus]
282: new FontButton(s_stringMgr.getString("laf.menus"),
283: _menuFontLbl, _prefs.getMenuFontInfo());
284: _staticFontBtn =
285: // i18n[laf.staticText=Static Text]
286: new FontButton(s_stringMgr.getString("laf.staticText"),
287: _staticFontLbl, _prefs.getStaticFontInfo());
288: _statusBarFontBtn =
289: // i18n[laf.statusBars=Status Bars]
290: new FontButton(s_stringMgr.getString("laf.statusBars"),
291: _statusBarFontLbl, _prefs.getStatusBarFontInfo());
292: _otherFontBtn =
293: // i18n[laf.other=Other]
294: new FontButton(s_stringMgr.getString("laf.other"),
295: _otherFontLbl, _prefs.getOtherFontInfo());
296:
297: FontButtonListener lis = new FontButtonListener();
298: _menuFontBtn.addActionListener(lis);
299: _staticFontBtn.addActionListener(lis);
300: _statusBarFontBtn.addActionListener(lis);
301: _otherFontBtn.addActionListener(lis);
302:
303: _menuFontEnabledChk.addActionListener(new ActionListener() {
304: public void actionPerformed(ActionEvent evt) {
305: _menuFontBtn.setEnabled(_menuFontEnabledChk
306: .isSelected());
307: }
308: });
309: _staticFontEnabledChk
310: .addActionListener(new ActionListener() {
311: public void actionPerformed(ActionEvent evt) {
312: _staticFontBtn
313: .setEnabled(_staticFontEnabledChk
314: .isSelected());
315: }
316: });
317: _statusBarFontEnabledChk
318: .addActionListener(new ActionListener() {
319: public void actionPerformed(ActionEvent evt) {
320: _statusBarFontBtn
321: .setEnabled(_statusBarFontEnabledChk
322: .isSelected());
323: }
324: });
325: _otherFontEnabledChk
326: .addActionListener(new ActionListener() {
327: public void actionPerformed(ActionEvent evt) {
328: _otherFontBtn
329: .setEnabled(_otherFontEnabledChk
330: .isSelected());
331: }
332: });
333:
334: JPanel pnl = new JPanel();
335: // i18n[laf.tabFonts=Fonts]
336: pnl.setBorder(BorderFactory.createTitledBorder(s_stringMgr
337: .getString("laf.tabFonts")));
338: pnl.setLayout(new GridBagLayout());
339: final GridBagConstraints gbc = new GridBagConstraints();
340: gbc.fill = GridBagConstraints.HORIZONTAL;
341: gbc.insets = new Insets(4, 4, 4, 4);
342:
343: gbc.gridx = 0;
344: gbc.gridy = 0;
345: pnl.add(_menuFontEnabledChk, gbc);
346:
347: ++gbc.gridy;
348: pnl.add(_staticFontEnabledChk, gbc);
349:
350: ++gbc.gridy;
351: pnl.add(_statusBarFontEnabledChk, gbc);
352:
353: ++gbc.gridy;
354: pnl.add(_otherFontEnabledChk, gbc);
355:
356: ++gbc.gridx;
357: gbc.gridy = 0;
358: pnl.add(_menuFontBtn, gbc);
359:
360: ++gbc.gridy;
361: pnl.add(_staticFontBtn, gbc);
362:
363: ++gbc.gridy;
364: pnl.add(_statusBarFontBtn, gbc);
365:
366: ++gbc.gridy;
367: pnl.add(_otherFontBtn, gbc);
368:
369: ++gbc.gridx;
370: gbc.gridy = 0;
371: gbc.fill = GridBagConstraints.HORIZONTAL;
372: gbc.weightx = 1.0;
373: pnl.add(_menuFontLbl, gbc);
374:
375: ++gbc.gridy;
376: pnl.add(_staticFontLbl, gbc);
377:
378: ++gbc.gridy;
379: pnl.add(_statusBarFontLbl, gbc);
380:
381: ++gbc.gridy;
382: pnl.add(_otherFontLbl, gbc);
383:
384: return pnl;
385: }
386:
387: private static final class FontButton extends JButton {
388: private FontInfo _fi;
389: private JLabel _lbl;
390: private Font _font;
391: private boolean _dirty;
392:
393: FontButton(String text, JLabel lbl, FontInfo fi) {
394: super (text);
395: _lbl = lbl;
396: _fi = fi;
397: }
398:
399: FontInfo getFontInfo() {
400: return _fi;
401: }
402:
403: Font getSelectedFont() {
404: return _font;
405: }
406:
407: void setSelectedFont(Font font) {
408: _font = font;
409: if (_fi == null) {
410: _fi = new FontInfo(font);
411: } else {
412: _fi.setFont(font);
413: }
414: _dirty = true;
415: }
416:
417: boolean isDirty() {
418: return _dirty;
419: }
420: }
421:
422: private static final class FontButtonListener implements
423: ActionListener {
424: public void actionPerformed(ActionEvent evt) {
425: if (evt.getSource() instanceof FontButton) {
426: FontButton btn = (FontButton) evt.getSource();
427: FontInfo fi = btn.getFontInfo();
428: Font font = null;
429: if (fi != null) {
430: font = fi.createFont();
431: }
432: font = new FontChooser().showDialog(font);
433: if (font != null) {
434: btn.setSelectedFont(font);
435: btn._lbl.setText(new FontInfo(font).toString());
436: }
437: }
438: }
439: }
440: }
441: }
|