View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.lang.reflect.Method;
30  import java.util.Properties;
31  
32  import javax.swing.UIManager;
33  import javax.swing.plaf.metal.MetalTheme;
34  
35  import org.apache.commons.cli.CommandLine;
36  import org.apache.commons.cli.CommandLineParser;
37  import org.apache.commons.cli.HelpFormatter;
38  import org.apache.commons.cli.Options;
39  import org.apache.commons.cli.ParseException;
40  import org.apache.commons.cli.PosixParser;
41  
42  import com.mindtree.techworks.insight.gui.action.LoadLocalFileAction;
43  import com.mindtree.techworks.insight.gui.preferences.PreferencesFrame;
44  import com.mindtree.techworks.insight.preferences.xmlpersistence.XMLPreferenceDataHandler;
45  
46  
47  /**
48   * This class is the main entry point to the Insight Application. This class
49   * also uses a command line interpertter to decide the behavior of Insight based
50   * on the parameters passed in.
51   * <p>
52   * The currently supported paramters are:
53   * <ul>
54   * 	<li><b>-ih</b>	Gives the INSIGHT_HOME value to use for the application</li>
55   * 	<li><b>-h</b>	Displays a Help</li>
56   * 	<li><b>-p</b>	Displays the Insight Preferences UI without the main Insight.</li>
57   * 	<li>			Default behavior: Display Insight GUI.
58   * </ul>
59   * </p>
60   *  
61   * @see com.mindtree.techworks.insight.gui.Insight
62   * 
63   * @author <a href="mailto:bindul_bhowmik@mindtree.com">Bindul Bhowmik</a>
64   * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
65   */
66  public class Insight {
67  
68  	/**
69  	 * The main entry point to the application, it parses the command line
70  	 * parameters and behaves accordingly.
71  	 * 
72  	 * @param args The command line arguments.
73  	 */
74  	public static void main (String [] args) {
75  
76  		// Initialize the parser with the available options
77  		CommandLineParser parser = new PosixParser();
78  		
79  		try {
80  			CommandLine line = parser.parse(getApplicationOptions(), args);
81  			
82  			// Check if the user has set the Insight Home
83  			String insightHome = null;
84  			if (line.hasOption("ih")) {
85  				insightHome = line.getOptionValue("ih");
86  			}
87  			
88  			if (line.hasOption("h")) {
89  				// Help
90  				HelpFormatter helpFormatter = new HelpFormatter();
91  				helpFormatter.printHelp(40, Insight.class.getName(),
92  						"MindTree Insight ver. "
93  								+ VersionUtil.getInsightVersion(),
94  						getApplicationOptions(), "Build Id: "
95  								+ VersionUtil.getInsightBuild()
96  								+ "\nWith Remote Protocol ver. "
97  								+ VersionUtil.getRemoteProtocolVersion(), true);
98  			} else if (line.hasOption("p")) {
99  				
100 				// Preferences
101 				
102 				// Initialize INSIGHT_HOME
103 				prepareToLaunch(insightHome);
104 				// Display the preferences frame
105 				PreferencesFrame preferencesFrame = new PreferencesFrame();
106 				preferencesFrame.showPreferences();
107 			} else if( line.hasOption("f")) {
108 				prepareToLaunch(insightHome);
109 				new com.mindtree.techworks.insight.gui.Insight();
110 				LoadLocalFileAction.getInstance().LoadOnFileSelection(line.getOptionValue("f"));
111 			} else {
112 				// Default behavior
113 				
114 				// Initialize INSIGHT_HOME
115 				prepareToLaunch(insightHome);
116 				new com.mindtree.techworks.insight.gui.Insight();
117 			}
118 			
119 		} catch (ParseException e) {
120 			System.out.println ("Unrecognized options in Insight.");
121 		}
122 
123 	}
124 	
125 	/**
126 	 * Prepares the application for launch
127 	 * @param insightHome The insight home
128 	 */
129 	private static void prepareToLaunch(String insightHome) {
130 		initializeInsightHome(insightHome);
131 		readDefaultProperties();
132 		setLookAndFeel();
133 	}
134 	
135 	/**
136 	 * Sets the look and feel of the application.
137 	 */
138 	private static void setLookAndFeel() {
139 		try {
140 			com.jgoodies.looks.Options.setPopupDropShadowEnabled(true);	  
141 			String lfClass = System.getProperty(InsightConstants.LOOK_AND_FEEL,
142 					"com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
143 			Class lfClazz = Class.forName(lfClass);
144 			Method themeMethod = lfClazz.getMethod("setCurrentTheme", new Class[]{MetalTheme.class});
145 			
146 			if (null != themeMethod) {
147 				String themeClass = System.getProperty(
148 						InsightConstants.LOOK_AND_FEEL_THEME,
149 						"com.jgoodies.looks.plastic.theme.SkyBlue");
150 				Class themeClazz = Class.forName(themeClass);
151 				themeMethod.invoke(null, new Object[] {themeClazz.newInstance()});
152 			}
153 	        UIManager.setLookAndFeel(lfClass);
154 	    } catch (Exception e) {
155 	    	e.printStackTrace();
156 	    }
157 	}
158 
159 	/**
160 	 * Initializes the INSIGHT_HOME system variable. The search for Insight
161 	 * home is executed in the following order:
162 	 * <ol>
163 	 * 	<li>The value passed in by the user</li>
164 	 * 	<li>Existing System variable - INSIGHT_HOME</li>
165 	 * 	<li>The user's home directory</li>
166 	 * </ol>
167 	 * @param insightHome
168 	 */
169 	private static void initializeInsightHome (String insightHome) {
170 
171 		if (insightHome == null) {
172 			// If the options does not have Insight Home, get it from the
173 			// System Properties
174 			insightHome = System.getProperty(InsightConstants.INSIGHT_HOME);
175 		}
176 		if(insightHome == null) {
177 			insightHome = System.getProperty("user.dir");
178 			System.setProperty(InsightConstants.INSIGHT_HOME, insightHome);
179 		}
180 		File insightPreferences = new File(insightHome + XMLPreferenceDataHandler.XML_PROP_FILE_URI);
181 		if (!insightPreferences.exists()) {
182 			System.err.println(InsightConstants.getLiteral("ERROR_INVALID_INSIGHT_HOME"));
183 			System.exit(1);
184 		}
185 		
186 	}
187 	
188 	/**
189 	 * Reads the insight.properties file from the classpath and sets the 
190 	 * properties in the System path
191 	 */
192 	private static void readDefaultProperties() {
193 		// Reads the default Insight properties
194 		InputStream propertiesInputStream = Thread.currentThread()
195 				.getContextClassLoader().getResourceAsStream(
196 						"insight.properties");
197 		Properties insightproperties = new Properties();
198 		try {
199 			insightproperties.load(propertiesInputStream);
200 			// TODO Add property resolution here
201 			// TODO Add property override here
202 			System.getProperties().putAll(insightproperties);
203 		} catch (IOException e) {
204 			System.err.println(InsightConstants.getLiteral("ERROR_INVALID_INSIGHT_PROPERTIES"));
205 			System.exit(1);
206 		}
207 	}
208 	
209 	/**
210 	 * Defines the various command line options available for the application.
211 	 * 
212 	 * @return An Options object used to parse the input.
213 	 */
214 	private static Options getApplicationOptions() {
215 		Options appOptions = new Options();
216 		
217 		// The Application
218 		appOptions.addOption("", false, "<No Arguments> Launches the Insight Application");
219 		
220 		// The Preference Option
221 		appOptions.addOption("p", "preferences", false, "Brings Up the " +
222 					"Preferences of the Insight instance");
223 		
224 		// The Help Option
225 		appOptions.addOption("h", "help", false, "Displays this help");
226 		
227 		// Insight Home
228 		appOptions.addOption("ih", "insight-home", true, "Set the Insight Home location");
229 		
230 		appOptions.addOption("f", "openfile", true, "Opens the specified log file with Insight");
231 		
232 		return appOptions;
233 	}
234 
235 }