KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > welcome > WelcomeTopComponent


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.welcome;
18
19 import org.mc4j.console.CreateConnectionAction;
20 import org.openide.ErrorManager;
21 import org.openide.awt.HtmlBrowser;
22 import org.openide.modules.InstalledFileLocator;
23 import org.openide.util.actions.SystemAction;
24 import org.openide.windows.TopComponent;
25
26 import javax.swing.*;
27 import javax.swing.event.HyperlinkEvent JavaDoc;
28 import javax.swing.event.HyperlinkListener JavaDoc;
29 import javax.swing.text.AttributeSet JavaDoc;
30 import javax.swing.text.Element JavaDoc;
31 import javax.swing.text.html.HTML JavaDoc;
32 import javax.swing.text.html.ObjectView JavaDoc;
33 import java.awt.*;
34 import java.awt.event.WindowAdapter JavaDoc;
35 import java.awt.event.WindowEvent JavaDoc;
36 import java.io.BufferedReader JavaDoc;
37 import java.io.File JavaDoc;
38 import java.io.FileFilter JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.io.InputStreamReader JavaDoc;
42 import java.io.Reader JavaDoc;
43 import java.net.MalformedURLException JavaDoc;
44 import java.net.URL JavaDoc;
45 import java.net.URLClassLoader JavaDoc;
46
47 /**
48  * @author Greg Hinkle (ghinkle@users.sourceforge.net), Oct 5, 2004
49  * @version $Revision: 570 $($Author: ghinkl $ / $Date: 2006-04-12 15:14:16 -0400 (Wed, 12 Apr 2006) $)
50  */

51 public class WelcomeTopComponent extends TopComponent {
52
53     private static WelcomeTopComponent instance;
54
55     public static void main(String JavaDoc[] args) {
56         JFrame frame = new JFrame("Test");
57         frame.getContentPane().add(new WelcomeTopComponent());
58         frame.setSize(600,600);
59         frame.setVisible(true);
60         frame.addWindowListener(new WindowAdapter JavaDoc() {
61             public void windowClosing(WindowEvent JavaDoc e) {
62                 System.exit(0);
63             }
64         });
65     }
66
67     public WelcomeTopComponent() {
68         initComponents();
69         setName("Welcome to MC4J");
70     }
71
72
73     public static WelcomeTopComponent getInstance() {
74         if (instance == null)
75             instance = new WelcomeTopComponent();
76
77         return instance;
78     }
79
80     public int getPersistenceType() {
81         return TopComponent.PERSISTENCE_ALWAYS;
82     }
83
84     protected String JavaDoc preferredID() {
85         return "welcomepanel";
86     }
87
88
89     public static class AliasedEditorPane extends JEditorPane {
90         protected void paintComponent(Graphics g) {
91             ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
92                                 RenderingHints.VALUE_ANTIALIAS_ON);
93             super.paintComponent(g);
94         }
95     }
96
97     private void initComponents() {
98         JPanel mainPanel = new JPanel();
99         mainPanel.setLayout(new BorderLayout());
100         javax.swing.JEditorPane JavaDoc htmlPane = new AliasedEditorPane();
101         htmlPane.setEditable(false);
102         htmlPane.setContentType("text/html");
103         htmlPane.setEditorKit(new CustomEditorKit());
104
105         try {
106             InputStream JavaDoc stream =
107                 getClass().getClassLoader().getResourceAsStream("org/mc4j/console/Welcome.html");
108
109
110             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(1000);
111             Reader JavaDoc r = new InputStreamReader JavaDoc(stream);
112
113             BufferedReader JavaDoc br = new BufferedReader JavaDoc(r);
114             String JavaDoc s = br.readLine();
115             while (s != null) {
116                 buf.append(s);
117                 s = br.readLine();
118             }
119
120             htmlPane.setText(buf.toString());
121         } catch (IOException JavaDoc e) {
122             e.printStackTrace();
123         }
124
125         htmlPane.addHyperlinkListener(new HyperlinkListener JavaDoc() {
126             public void hyperlinkUpdate(HyperlinkEvent JavaDoc e) {
127                 if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
128                     System.out.println("you clicked: " + e.getDescription());
129
130                     if ("newconnection".equals(e.getDescription())) {
131                         SystemAction action = SystemAction.get(CreateConnectionAction.class);
132                         action.actionPerformed(null);
133                     } else {
134                         URL JavaDoc url = e.getURL();
135                         HtmlBrowser.URLDisplayer.getDefault().showURL(url);
136                     }
137                 }
138             }
139         });
140
141
142         final JScrollPane scrollPane = new JScrollPane(htmlPane);
143         mainPanel.add(scrollPane, BorderLayout.CENTER);
144
145         setLayout(new BorderLayout());
146         add(mainPanel, BorderLayout.CENTER);
147
148         SwingUtilities.invokeLater(new Runnable JavaDoc() {
149             public void run() {
150                 scrollPane.getVerticalScrollBar().setValue(0);
151                     //scrollRectToVisible(new Rectangle(0,0,1,1));
152
}
153         });
154
155     }
156
157
158
159
160     public static class CustomObjectView extends ObjectView JavaDoc {
161         public CustomObjectView(Element JavaDoc elem) {
162             super(elem);
163         }
164
165         protected Component createComponent() {
166             int width = -1, height = -1;
167
168             try {
169                 width = Integer.parseInt((String JavaDoc) getElement().getAttributes().getAttribute(HTML.Attribute.WIDTH));
170                 height = Integer.parseInt((String JavaDoc) getElement().getAttributes().getAttribute(HTML.Attribute.HEIGHT));
171             } catch(Exception JavaDoc e) {}
172
173
174
175
176             Component comp = null;
177             AttributeSet JavaDoc attr = getElement().getAttributes();
178             String JavaDoc classname = (String JavaDoc) attr.getAttribute(HTML.Attribute.CLASSID);
179             Class JavaDoc c = null;
180 // try {
181
// c = Class.forName(classname);
182
// } catch (ClassNotFoundException e) {
183
// ClassLoader loader = getClassLoader();
184
try {
185                     c = Class.forName(classname);
186                 } catch (ClassNotFoundException JavaDoc e1) { }
187 // }
188
if (c == null) {
189                 return new JLabel("??");
190             }
191             try {
192                 Object JavaDoc o = c.newInstance();
193                 if (o instanceof Component) {
194                     comp = (Component) o;
195                 }
196             } catch (IllegalAccessException JavaDoc e) {
197                 e.printStackTrace();
198             } catch (InstantiationException JavaDoc e) {
199                 e.printStackTrace();
200             }
201
202
203             if (width > 0 && height > 0 && comp instanceof JComponent)
204                 ((JComponent)comp).setPreferredSize(new Dimension(width, height));
205
206             return comp;
207         }
208
209         private ClassLoader JavaDoc getClassLoader() {
210             // TODO GH: This is a hack. We're making sure we've got the animation library
211
// in our classpath. Should probably change such that all mc4jlib/common libraries
212
// are loaded in the Class-Path: reference in the manafest.mf
213
File JavaDoc extrasFile =
214                 InstalledFileLocator.getDefault().locate("mc4jlib/mc4j_common.jar","org.mc4j.console",false);
215             if (extrasFile != null && !extrasFile.exists()) {
216                 ErrorManager.getDefault().notify(
217                     new RuntimeException JavaDoc("Unable to locate mc4j_common.jar in the mc4jlib folder")
218                 );
219             }
220
221             File JavaDoc mc4jLibDir = extrasFile.getParentFile();
222
223             File JavaDoc[] commonLibs = mc4jLibDir.listFiles(new FileFilter JavaDoc() {
224                 public boolean accept(File JavaDoc file) {
225                     return (!file.isDirectory() &&
226                         (file.getName().toLowerCase().endsWith(".jar") ||
227                         file.getName().toLowerCase().endsWith(".zip")));
228                 }
229             });
230             URL JavaDoc[] urls = new URL JavaDoc[commonLibs.length];
231             for (int i = 0; i < commonLibs.length; i++) {
232                 File JavaDoc commonLib = commonLibs[i];
233                 try {
234                     urls[i] = commonLib.toURL();
235                 } catch (MalformedURLException JavaDoc e) {
236                     e.printStackTrace();
237                 }
238             }
239             ClassLoader JavaDoc loader = new URLClassLoader JavaDoc(urls);
240             return loader;
241         }
242     }
243
244
245 }
246
Popular Tags