001: package com.xoetrope.carousel.langed;
002:
003: import java.net.URL;
004: import java.util.Date;
005: import java.util.prefs.Preferences;
006:
007: //import com.websina.license.LicenseManager;
008: import java.awt.Component;
009: import java.awt.Container;
010: import java.awt.Dimension;
011: import java.awt.Font;
012: import java.awt.Frame;
013: import java.awt.Rectangle;
014: import java.awt.Toolkit;
015: import javax.swing.ImageIcon;
016: import javax.swing.JLabel;
017: import javax.swing.JOptionPane;
018: import javax.swing.JWindow;
019: import javax.swing.SwingUtilities;
020: import javax.swing.LookAndFeel;
021: import javax.swing.UIManager;
022:
023: /**
024: * <p>Title: LanguageEditor</p>
025: * <p>Description: Language Resource Translation Utility</p>
026: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
027: * the GNU Public License (GPL), please see license.txt for more details. If
028: * you make commercial use of this software you must purchase a commercial
029: * license from Xoetrope.</p>
030: * <p> $Revision: 1.6 $</p>
031: * @author Luan O'Carroll
032: */
033: public class LanguageEditorApplication {
034: private JWindow splashScreen = null;
035: private boolean packFrame = false;
036: private LangFrame frame = null;
037: private static boolean validLicense = false;
038: private static final long twentyEightDays = 2419200000l; //1000*3600*24*28;
039: public static Font defaultFont;
040: static boolean isPlugin = false;
041:
042: //Construct the application
043: public LanguageEditorApplication(Frame appFrame,
044: boolean isApplication, boolean showSplash) {
045:
046: // Create and throw the splash screen up. Since this will
047: // physically throw bits on the screen, we need to do this
048: // on the GUI thread using invokeLater.
049: if (showSplash)
050: createSplashScreen(appFrame);
051:
052: init(isApplication);
053: validLicense = checkLicense();
054: if (!validLicense) {
055: JOptionPane
056: .showMessageDialog(
057: frame,
058: "A valid license was not found.\r\n XTranslator will run in evaluation mode and you will not be able to save languages of more than 25 strings.\r\nYour evaluation is valid for a total of 28 days. A license can be purchased for http://www.xoetrope.net",
059: "Licensing Error",
060: JOptionPane.WARNING_MESSAGE);
061:
062: try {
063: Preferences prefs = Preferences
064: .userNodeForPackage(LangEdDesktop.class);
065: Date today = new Date();
066: Date defaultDate = new Date(today.getTime()
067: + twentyEightDays);
068: String lastUsed = prefs.get("expiry", Long
069: .toHexString(defaultDate.getTime()));
070: Date expiryDate = new Date(Long.parseLong(lastUsed, 16));
071:
072: if (expiryDate.compareTo(today) < 0) {
073: JOptionPane
074: .showMessageDialog(
075: frame,
076: "No license was found and the evaluation period has expired.\r\nA license can be purchased for http://www.xoetrope.net",
077: "Licensing Expired",
078: JOptionPane.ERROR_MESSAGE);
079: System.exit(0);
080: }
081: prefs.put("expiry", Long.toHexString(expiryDate
082: .getTime()));
083: } catch (Exception ex) {
084: System.exit(0);
085: }
086: }
087: }
088:
089: private void init(boolean isApplication) {
090: try {
091: if (isApplication)
092: frame = new LangFrame();
093: } catch (Exception ex) {
094: ex.printStackTrace();
095: }
096:
097: //Validate frames that have preset sizes
098: //Pack frames that have useful preferred size info, e.g. from their layout
099: if (frame != null) {
100: if (packFrame)
101: frame.pack();
102: else
103: frame.validate();
104:
105: //Center the window
106: Dimension screenSize = Toolkit.getDefaultToolkit()
107: .getScreenSize();
108: Dimension frameSize = frame.getSize();
109: if (frameSize.height > screenSize.height)
110: frameSize.height = screenSize.height;
111:
112: if (frameSize.width > screenSize.width)
113: frameSize.width = screenSize.width;
114:
115: frame.setLocation((screenSize.width - frameSize.width) / 2,
116: (screenSize.height - frameSize.height) / 2);
117: frame.setVisible(true);
118: }
119: }
120:
121: //Main method
122: public static void main(String[] args) {
123: LangFrame.isStandalone = true;
124: try {
125: UIManager.setLookAndFeel(UIManager
126: .getSystemLookAndFeelClassName());
127: //UIManager.setLookAndFeel( new javax.swing.plaf.metal.MetalLookAndFeel());
128: // com.incors.plaf.kunststoff.KunststoffLookAndFeel lf = new com.incors.plaf.kunststoff.KunststoffLookAndFeel();
129: // lf.setCurrentTheme( new XoetropeTheme());
130: // lf.setCurrentGradientTheme( new XoetropeGradientTheme());
131: // UIManager.setLookAndFeel( lf );
132: } catch (Exception e) {
133: e.printStackTrace();
134: }
135:
136: LanguageEditorApplication le = new LanguageEditorApplication(
137: null, true, true);
138: }
139:
140: /**
141: * Show the spash screen while the rest of the demo loads
142: */
143: public void createSplashScreen(Frame appFrame) {
144: URL image = null;
145: JLabel splashLabel = null;
146: try {
147: LanguageEditorApplication.class.getResource("/splash.jpg");
148: splashLabel = new JLabel(new ImageIcon(image));
149: } catch (Exception e) {
150: splashLabel = new JLabel();
151: }
152:
153: Frame frame = appFrame;
154: if (frame == null)
155: frame = new Frame();
156: splashScreen = new JWindow(frame);
157: splashScreen.getContentPane().add(splashLabel);
158: splashScreen.pack();
159: Rectangle screenRect = frame.getGraphicsConfiguration()
160: .getBounds();
161: splashScreen.setLocation(screenRect.x + screenRect.width / 2
162: - splashScreen.getSize().width / 2, screenRect.y
163: + screenRect.height / 2 - splashScreen.getSize().height
164: / 2);
165:
166: // do the following on the gui thread
167: SwingUtilities.invokeLater(new Runnable() {
168: public void run() {
169: showSplashScreen();
170: }
171: });
172: new SplashSleeperThread(this ).start();
173: }
174:
175: private void showSplashScreen() {
176: if (splashScreen != null) {
177: splashScreen.setVisible(true);
178: splashScreen.repaint();
179: }
180: }
181:
182: /**
183: * pop down the spash screen
184: */
185: public void hideSplash() {
186: if (splashScreen != null) {
187: splashScreen.setVisible(false);
188: splashScreen = null;
189: }
190: }
191:
192: private boolean checkLicense() {
193: // try {
194: // LicenseManager manager = null;
195: // try
196: // {
197: // java.net.URL url = LanguageEditor.class.getResource("/license.lic" );
198: // InputStream is = ClassLoader.getSystemResourceAsStream( "license.lic" );
199: // if ( is == null ) {
200: // System.out.println("License not loaded from stream");
201: // if ( url != null )
202: // return true;
203: // return false;
204: // }
205: //
206: // // first get a instance of the LicenseManager.
207: // manager= LicenseManager.getInstance();
208: // }
209: // catch(Exception exception)
210: // {
211: // System.out.println( "No license file found" );
212: // }
213: // // you then validate the license by calling isValid().
214: // boolean valid = manager.isValid();
215: //
216: // String localhost = manager.getFeature( "IP Address" );
217: // String application = manager.getFeature( "Product" );
218: // String userName = manager.getFeature( "User Name" );
219: // String licensee = manager.getFeature( "Licensee" );
220: // if ( valid ) {
221: // if ( application.indexOf( "XTranslator" ) != 0 )
222: // return false;
223: // if ( licensee.compareTo( "Danfoss A/S" ) == 0 ) {
224: // System.out.println( "The application is running under a license granted exclusively to Danfoss A/S, Nordborg, Denmark" );
225: // return true;
226: // }
227: //
228: // if (( InetAddress.getLocalHost().toString().indexOf( localhost ) < 0 ) && ( System.getProperty( "user.name" ).indexOf( userName ) < 0 )) {
229: // System.out.println( "The host name does not match the license file: " +userName + ":" +System.getProperty( "user.name" ) + ":" + InetAddress.getLocalHost().toString());
230: // return false;
231: // }
232: //
233: // System.out.println("License is valid: "+valid);
234: // // also you can get the days left by calling daysLeft().
235: // System.out.println("Days left: "+manager.daysLeft());
236: // return true;
237: // }
238: // else {
239: // System.out.println("license is invalid and so the program exits....");
240: // //exit the program
241: // }
242: // }
243: // catch (Exception ex) {
244: // ex.printStackTrace();
245: // }
246: // return false;
247: return true;
248: }
249:
250: static boolean isEvaluation() {
251: return !validLicense;
252: }
253:
254: public void setDefaultFont(Font font) {
255: defaultFont = font;
256: }
257:
258: void setIsPlugin(boolean _isPlugin) {
259: isPlugin = _isPlugin;
260: }
261:
262: /**
263: * Set the default font for all the components in this container.
264: * @param cont the container
265: */
266: public static void setDefaultFont(Container cont) {
267: if (defaultFont != null) {
268: cont.setFont(defaultFont);
269:
270: int numComponents = cont.getComponentCount();
271: for (int i = 0; i < numComponents; i++) {
272: Component c = cont.getComponent(i);
273:
274: if (c instanceof Container)
275: setDefaultFont((Container) c);
276: else
277: c.setFont(defaultFont);
278: }
279: }
280: }
281: }
282:
283: class SplashSleeperThread extends Thread {
284: private LanguageEditorApplication langEditor;
285:
286: public SplashSleeperThread(LanguageEditorApplication le) {
287: langEditor = le;
288: }
289:
290: public void run() {
291: try {
292: sleep(5000);
293: } catch (InterruptedException ex) {
294: }
295: langEditor.hideSplash();
296: }
297: }
|