1 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 ; 28 import javax.swing.event.HyperlinkListener ; 29 import javax.swing.text.AttributeSet ; 30 import javax.swing.text.Element ; 31 import javax.swing.text.html.HTML ; 32 import javax.swing.text.html.ObjectView ; 33 import java.awt.*; 34 import java.awt.event.WindowAdapter ; 35 import java.awt.event.WindowEvent ; 36 import java.io.BufferedReader ; 37 import java.io.File ; 38 import java.io.FileFilter ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 import java.io.InputStreamReader ; 42 import java.io.Reader ; 43 import java.net.MalformedURLException ; 44 import java.net.URL ; 45 import java.net.URLClassLoader ; 46 47 51 public class WelcomeTopComponent extends TopComponent { 52 53 private static WelcomeTopComponent instance; 54 55 public static void main(String [] 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 () { 61 public void windowClosing(WindowEvent 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 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 htmlPane = new AliasedEditorPane(); 101 htmlPane.setEditable(false); 102 htmlPane.setContentType("text/html"); 103 htmlPane.setEditorKit(new CustomEditorKit()); 104 105 try { 106 InputStream stream = 107 getClass().getClassLoader().getResourceAsStream("org/mc4j/console/Welcome.html"); 108 109 110 StringBuffer buf = new StringBuffer (1000); 111 Reader r = new InputStreamReader (stream); 112 113 BufferedReader br = new BufferedReader (r); 114 String 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 e) { 122 e.printStackTrace(); 123 } 124 125 htmlPane.addHyperlinkListener(new HyperlinkListener () { 126 public void hyperlinkUpdate(HyperlinkEvent 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 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 () { 149 public void run() { 150 scrollPane.getVerticalScrollBar().setValue(0); 151 } 153 }); 154 155 } 156 157 158 159 160 public static class CustomObjectView extends ObjectView { 161 public CustomObjectView(Element elem) { 162 super(elem); 163 } 164 165 protected Component createComponent() { 166 int width = -1, height = -1; 167 168 try { 169 width = Integer.parseInt((String ) getElement().getAttributes().getAttribute(HTML.Attribute.WIDTH)); 170 height = Integer.parseInt((String ) getElement().getAttributes().getAttribute(HTML.Attribute.HEIGHT)); 171 } catch(Exception e) {} 172 173 174 175 176 Component comp = null; 177 AttributeSet attr = getElement().getAttributes(); 178 String classname = (String ) attr.getAttribute(HTML.Attribute.CLASSID); 179 Class c = null; 180 try { 185 c = Class.forName(classname); 186 } catch (ClassNotFoundException e1) { } 187 if (c == null) { 189 return new JLabel("??"); 190 } 191 try { 192 Object o = c.newInstance(); 193 if (o instanceof Component) { 194 comp = (Component) o; 195 } 196 } catch (IllegalAccessException e) { 197 e.printStackTrace(); 198 } catch (InstantiationException 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 getClassLoader() { 210 File 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 ("Unable to locate mc4j_common.jar in the mc4jlib folder") 218 ); 219 } 220 221 File mc4jLibDir = extrasFile.getParentFile(); 222 223 File [] commonLibs = mc4jLibDir.listFiles(new FileFilter () { 224 public boolean accept(File file) { 225 return (!file.isDirectory() && 226 (file.getName().toLowerCase().endsWith(".jar") || 227 file.getName().toLowerCase().endsWith(".zip"))); 228 } 229 }); 230 URL [] urls = new URL [commonLibs.length]; 231 for (int i = 0; i < commonLibs.length; i++) { 232 File commonLib = commonLibs[i]; 233 try { 234 urls[i] = commonLib.toURL(); 235 } catch (MalformedURLException e) { 236 e.printStackTrace(); 237 } 238 } 239 ClassLoader loader = new URLClassLoader (urls); 240 return loader; 241 } 242 } 243 244 245 } 246 | Popular Tags |