001 /*
002 * steelme theme manager for Java
003 * Copyright (C) 2000 - 2004 T.J. Willis
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
018 *
019 * $Header: /cvsroot/steelme/steelme/src/net/sourceforge/steelme/ThemeMenu.java,v 1.3 2004/05/27 23:11:09 tj_willis Exp $
020 */
021 package net.sourceforge.steelme;
022
023 import java.awt.*;
024 import java.awt.event.*;
025 import javax.swing.*;
026 import javax.swing.plaf.*;
027 import javax.swing.plaf.metal.*;
028 import java.io.*;
029 import java.util.*;
030 import org.apache.log4j.*;
031
032 /**
033 * Describe class <code>ThemeMenu</code> here.
034 *
035 *@author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a>
036 *@created May 31, 2004
037 *@version 1.0
038 *@has 1..n Has - net.sourceforge.steelme.ThemeResistantJMenuItem
039 *@has 1..n Has - net.sourceforge.steelme.SteelmeTheme
040 *@has 1 Has - net.sourceforge.steelme.ThemeFileFilter
041 */
042 public class ThemeMenu
043 extends JMenu
044 implements ActionListener {
045 private JComponent targ;
046 private JMenuItem editor;
047 private JMenuItem reload;
048 private static Category cat
049 = Category.getInstance(SteelmeTheme.class.getName());
050 private File themeDir;
051
052
053 /**
054 * Creates a new <code>ThemeMenu</code> instance.
055 *
056 *@throws IllegalArgumentException if themeDir isn't a directory or has
057 * no files.
058 *@param target a <code>JFrame</code> value
059 *@param themeDir a <code>File</code> value
060 */
061 public ThemeMenu(JComponent target, File themeDir) {
062 super("Themes");
063 targ = target;
064 this.themeDir = themeDir;
065 init();
066 }
067
068
069 /**
070 * Adds (or re-adds) all installed themes, editor item, and reload item.
071 *
072 * @throws IllegalArgumentException if themeDir isn't a directory or has
073 * no files.
074 */
075 private void init() {
076 //assert themeDir.isDirectory(): "Not a directory";
077 if (!themeDir.isDirectory()) {
078 throw new IllegalArgumentException("Given directory " + themeDir + " is not a directory");
079 }
080 File fileArr[] = themeDir.listFiles();
081 if (fileArr == null) {
082 throw new IllegalArgumentException("Given directory " + themeDir + " has no files");
083 }
084 int mySize = java.lang.reflect.Array.getLength(fileArr);
085 if (mySize == 0) {
086 //FIXME: might be redundant, check dir with 0 files
087 throw new IllegalArgumentException("Given directory " + themeDir + " has no files");
088 }
089 this.removeAll();
090 JMenu sub = new JMenu("Installed Themes");
091 add(sub);
092 ThemeFileFilter filter = new ThemeFileFilter();
093
094 //for(File f: fileArr) {
095 for (int i = 0; i < mySize; i++) {
096 File f = fileArr[i];
097 if (f == null || f.isDirectory()) {
098 continue;
099 }
100 if ((filter != null) && (!filter.accept(f))) {
101 continue;
102 }
103
104 try {
105 SteelmeTheme t = new SteelmeTheme(f);
106 if (t == null) {
107 continue;
108 }
109 ThemeResistantJMenuItem it
110 = new ThemeResistantJMenuItem(t.getName());
111 it.setMyBackground(t.getS3());
112 it.setMyForeground(t.getT1());
113 it.setMyFont(t.getControlTextFont());
114 it.setActionCommand(f.getCanonicalPath());
115 it.addActionListener(this);
116 sub.add(it);
117 } catch (Exception ex) {
118 cat.warn("Theme loading exception: " + f, ex);
119 }
120 }
121 reload = new JMenuItem("Reload Themes");
122 add(reload);
123 reload.addActionListener(this);
124 editor = new JMenuItem("Theme Editor");
125 add(editor);
126 editor.addActionListener(this);
127 }
128
129
130 /**
131 * Adds a feature to the Frame attribute of the ThemeMenu object
132 *
133 *@param f The feature to be added to the Frame attribute
134 */
135 private void addFrame(JFrame frame) {
136 frame.setVisible(true);
137 frame.pack();
138 }
139
140
141 /**
142 * Describe <code>actionPerformed</code> method here.
143 *
144 *@param c an <code>ActionEvent</code> value
145 */
146 public void actionPerformed(ActionEvent evt) {
147 Object source = evt.getSource();
148
149 if (source.equals(editor)) {
150 ThemeEditor editor = new ThemeEditor(targ.getRootPane());
151 addFrame(editor);
152 javax.swing.SwingUtilities.updateComponentTreeUI(targ.getRootPane());
153 } else if (source.equals(reload)) {
154 init();
155 } else {
156 File themeFile = new File(evt.getActionCommand());
157 SteelmeTheme theme = new SteelmeTheme(themeFile);
158 SteelmeThemeManager.setCurrentTheme(theme);
159 if (targ != null) {
160 theme.applyTheme(targ);
161 }
162 init();
163 }
164 }
165 }
166
167