Author: tfmorris
Date: 2007-11-17 18:38:13-0800
New Revision: 13783
Modified:
trunk/src_new/org/argouml/util/ConfigLoader.java
Log:
One more refactoring to simplify, but also deprecate all usage.
Modified: trunk/src_new/org/argouml/util/ConfigLoader.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src_new/org/argouml/util/ConfigLoader.java?view=diff&rev=13783&p1=trunk/src_new/org/argouml/util/ConfigLoader.java&p2=trunk/src_new/org/argouml/util/ConfigLoader.java&r1=13782&r2=13783
==============================================================================
--- trunk/src_new/org/argouml/util/ConfigLoader.java (original)
+++ trunk/src_new/org/argouml/util/ConfigLoader.java 2007-11-17 18:38:13-0800
@@ -31,9 +31,12 @@
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
+import javax.swing.JPanel;
+
import org.apache.log4j.Logger;
import org.argouml.application.api.Argo;
import org.argouml.configuration.Configuration;
@@ -41,12 +44,11 @@
import org.tigris.swidgets.Orientation;
/**
- * This class loads panel classes specified by a configuration file.
+ * This class loads property panel tab classes specified by a configuration file.
*/
public class ConfigLoader {
- private static final Logger LOG =
- Logger.getLogger(ConfigLoader.class);
+ private static final Logger LOG = Logger.getLogger(ConfigLoader.class);
private static final String CONFIG_FILE_PROPERTY = "argo.config";
private static final String DEFAULT_CONFIG_FILE = "/org/argouml/argo.ini";
@@ -55,7 +57,10 @@
/**
* @return the orientation
+ * @deprecated for 0.25.4 by tfmorris. Use this.getOrientation() on the
+ * parent component who's orientation you want to match.
*/
+ @Deprecated
public static Orientation getTabPropsOrientation() {
return tabPropsOrientation;
}
@@ -68,7 +73,11 @@
* @param tabs List in which to return the tabs that were loaded.
* @param panelName the panel name
* @param orientation the orientation (Horizontal or Vertical)
+ * @deprecated for 0.25.4 by tfmorris. Callers should instantiate
+ * required classes directly rather than allowing dependencies to be
+ * hidden in argo.ini.
*/
+ @Deprecated
public static void loadTabs(List tabs, String panelName,
final Orientation orientation) {
@@ -81,9 +90,36 @@
panelName = "detail";
}
+ List<Class<JPanel>> tabClasses = readConfigFile(panelName, orientation,
+ position);
+
+ if (tabClasses != null) {
+ for (Class<JPanel> tabClass : tabClasses) {
+ JPanel tab = createTab(tabClass, position);
+ if (tab != null) {
+ tabs.add(tab);
+ }
+ }
+ updateOrientation(tabs, orientation);
+ }
+ }
+
+
+ /**
+ * Read each line from the config file, parsing each line and adding
+ * the relevant tabs.
+ *
+ * @param tabs List of tabs which were added.
+ * @param panelName
+ * @param orientation
+ * @param position
+ */
+ private static List<Class<JPanel>> readConfigFile(String panelName,
+ Orientation orientation, String position) {
+
InputStream is = null;
- LineNumberReader lnr = null;
- String configFile = System.getProperty(CONFIG_FILE_PROPERTY);
+ LineNumberReader lnr = null;
+ String configFile = System.getProperty(CONFIG_FILE_PROPERTY);
//
// if property specified
//
@@ -106,45 +142,25 @@
configFile = DEFAULT_CONFIG_FILE;
is = ConfigLoader.class.getResourceAsStream(configFile);
}
- if (is != null) {
- try {
- lnr = new LineNumberReader(new InputStreamReader(is,
- Argo.getEncoding()));
- } catch (UnsupportedEncodingException ueExc) {
- lnr = new LineNumberReader(new InputStreamReader(is));
- }
-
- if (lnr != null) {
- readConfigFile(tabs, panelName, orientation, position, lnr,
- configFile);
- }
- else {
+ if (is == null) {
LOG.error("Unable to instantiate a config file reader");
- }
+ return null;
+ }
+ try {
+ lnr = new LineNumberReader(new InputStreamReader(is,
+ Argo.getEncoding()));
+ } catch (UnsupportedEncodingException ueExc) {
+ lnr = new LineNumberReader(new InputStreamReader(is));
}
- }
- /**
- * Read each line from the config file, parsing each line and adding
- * the relevant tabs.
- *
- * @param tabs List of tabs which were added.
- * @param panelName
- * @param orientation
- * @param position
- * @param lnr reader for the config file
- * @param configFile name of the config file (used for error reporting)
- */
- private static void readConfigFile(List tabs, String panelName,
- Orientation orientation, String position, LineNumberReader lnr,
- String configFile) {
+ List<Class<JPanel>> tabs = new ArrayList<Class<JPanel>>();
try {
String line = lnr.readLine();
while (line != null) {
- Class tabClass = parseConfigLine(line, panelName, lnr
- .getLineNumber(), configFile);
+ Class tabClass = parseConfigLine(line, panelName,
+ lnr.getLineNumber(), configFile);
if (tabClass != null) {
- addTab(tabs, orientation, position, tabClass);
+ tabs.add(tabClass);
}
line = lnr.readLine();
}
@@ -152,32 +168,23 @@
catch (IOException io) {
LOG.error(io);
}
+ return tabs;
}
/**
- * Instantiates the class for a tab and adds it to the list of loaded tabs.
- *
- * @param tabs list of tabs which have been added.
- * @param orientation orientation of the TabProps tab
- * @param position
+ * Instantiates the class for a tab if its saved position matches the
+ * requested position. Also saves the orientation of the TabProps tab
+ * when it is encountered.
* @param tabClass the class to be instantiated as a new tab
+ * @param position requested position (North, South, etc)
*/
- private static void addTab(List tabs, Orientation orientation,
- String position, Class tabClass) {
+ private static JPanel createTab(Class<JPanel> tabClass, String position) {
try {
String className = tabClass.getName();
- String shortClassName = className.substring(
- className.lastIndexOf('.') + 1).toLowerCase();
- ConfigurationKey key = Configuration.makeKey("layout",
- shortClassName);
if (position == null
- || position.equalsIgnoreCase(Configuration
- .getString(key, "South"))) {
- if (className.equals("org.argouml.uml.ui.TabProps")) {
- tabPropsOrientation = orientation;
- }
- Object newTab = tabClass.newInstance();
- tabs.add(newTab);
+ || position.equalsIgnoreCase(getSavedPosition(className))) {
+ JPanel newTab = tabClass.newInstance();
+ return newTab;
}
} catch (InstantiationException ex) {
LOG.error("Could not make instance of "
@@ -186,12 +193,23 @@
LOG.error("Could not make instance of "
+ tabClass.getName());
}
+ return null;
+ }
+
+ /**
+ * @return the saved position for the given class
+ */
+ private static String getSavedPosition(String className) {
+ String shortClassName = className.substring(
+ className.lastIndexOf('.') + 1).toLowerCase();
+ ConfigurationKey key = Configuration.makeKey("layout", shortClassName);
+ return Configuration.getString(key, "South");
}
/**
* Parse a line in the text file containing the configuration of ArgoUML,
- * "/org/argouml/argo.ini". Also has the side effect of loading the
- * named class if one is found.
+ * "/org/argouml/argo.ini". Also has the side effect of loading the named
+ * class if one is found.
*
* @param line the given line
* @param panelName the name of the panel
@@ -238,8 +256,8 @@
return res;
}
}
- if (Boolean.getBoolean("dbg")) {
- LOG.warn("\nCould not find any of these classes:\n"
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("\nCould not find any of these classes:\n"
+ "TabPath=" + tabPath + "\n"
+ "Config file=" + configFile + "\n"
+ "Config line #" + lineNum + ":" + line);
@@ -252,9 +270,18 @@
* @param s input string
* @return string with everything before (including) colon stripped
*/
- public static String stripBeforeColon(String s) {
+ private static String stripBeforeColon(String s) {
int colonPos = s.indexOf(":");
return s.substring(colonPos + 1);
}
+ private static void updateOrientation(List<JPanel> tabs,
+ Orientation orientation) {
+ for (JPanel tab : tabs) {
+ if (tab instanceof org.argouml.uml.ui.TabProps) {
+ tabPropsOrientation = orientation;
+ }
+ }
+ }
+
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.