|
| 1 | +package com.polytopemedia; |
| 2 | +import java.awt.Color; |
| 3 | +import java.awt.Dimension; |
| 4 | +import java.awt.event.ActionEvent; |
| 5 | +import java.awt.event.ActionListener; |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.File; |
| 8 | +import java.io.FileNotFoundException; |
| 9 | +import java.io.FileReader; |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +import javax.swing.JApplet; |
| 13 | +import javax.swing.JButton; |
| 14 | +import javax.swing.JFrame; |
| 15 | +import javax.swing.JOptionPane; |
| 16 | +import javax.swing.JPanel; |
| 17 | + |
| 18 | +import com.polytopemedia.polyhedra.nets.NameAndNet; |
| 19 | +import com.polytopemedia.polyhedra.nets.Net; |
| 20 | +import com.polytopemedia.polyhedra.nets.NetBuilder; |
| 21 | +import com.polytopemedia.polyhedra.ui.ImagePanel; |
| 22 | +import com.polytopemedia.polyhedra.ui.MainPanel; |
| 23 | +import com.polytopemedia.polyhedra.ui.MarkColors; |
| 24 | +import com.polytopemedia.polyhedra.ui.io.FileUtils; |
| 25 | +import com.polytopemedia.polyhedra.ui.io.JSCallback; |
| 26 | +import com.polytopemedia.polyhedra.ui.io.PolyhedronFileFilter; |
| 27 | +import com.polytopemedia.polyhedra.utils.Utils; |
| 28 | + |
| 29 | + |
| 30 | +public class PolyhedronApplet extends JApplet implements ActionListener{ |
| 31 | + private String encodedNet; |
| 32 | + private String name; |
| 33 | + |
| 34 | + public static boolean isJS = false; |
| 35 | + public static String myURL = "http://www.dr-mikes-math-games-for-kids.com/polyhedral-nets.html?"; |
| 36 | + |
| 37 | + static { |
| 38 | + boolean isJS = false; |
| 39 | + String url = myURL; |
| 40 | + /** |
| 41 | + * -- // BH getting the URL directly |
| 42 | + * |
| 43 | + * @j2sNative |
| 44 | + * |
| 45 | + * isJS = true; |
| 46 | + * url = document.location.href.split("#")[0]; |
| 47 | + */ |
| 48 | + {} |
| 49 | + PolyhedronApplet.isJS = isJS; |
| 50 | + if (url != null) |
| 51 | + PolyhedronApplet.myURL = url; |
| 52 | + } |
| 53 | + |
| 54 | + public void start() { |
| 55 | + |
| 56 | + JPanel panel = ImagePanel.defaultImage(); |
| 57 | + this.setContentPane(panel); |
| 58 | + NetBuilder netBuilder = new NetBuilder(); |
| 59 | + |
| 60 | + encodedNet = getParameterOrQuery("net"); // BH |
| 61 | + name = getParameterOrQuery("name"); // BH |
| 62 | + |
| 63 | + // BH trim down the url now, not necessarily removing other parameters |
| 64 | + // that come prior to net= and name= |
| 65 | + myURL = myURL.split("#")[0].split("net=")[0].split("name=")[0]; |
| 66 | + if (myURL.endsWith("?")) |
| 67 | + myURL = myURL.substring(0, myURL.length() - 1); |
| 68 | + |
| 69 | + if (name != null && name.length() == 0) { |
| 70 | + name = null; |
| 71 | + } |
| 72 | + if (Utils.containsRudeWord(name)) { |
| 73 | + name = null; |
| 74 | + } |
| 75 | + try { |
| 76 | + netBuilder.decode(encodedNet); |
| 77 | + } catch (NullPointerException e) { |
| 78 | + encodedNet = null; |
| 79 | + name = null; |
| 80 | + } |
| 81 | + |
| 82 | + addButton(panel, "New Net", "Create a new polyhedral net"); |
| 83 | + if (FileUtils.canAccessFileSystem()) { |
| 84 | + addButton(panel, "Load Net", "Load a saved polyhedral net from your hard disk"); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + if (encodedNet != null) { |
| 89 | + addButton(panel, "Linked Net", "Show the linked polyhedral net"+(name == null ? "" : ", \""+name+"\"")); |
| 90 | + showMainPanel(netBuilder.decode(encodedNet), name); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Check query first, then parameters, for a key=value pair |
| 96 | + * |
| 97 | + * @param key |
| 98 | + * @return value or null |
| 99 | + * @author Bob Hanson |
| 100 | + */ |
| 101 | + private String getParameterOrQuery(String key) { |
| 102 | + // split out the key/value pairs in the query |
| 103 | + String query = getDocumentBase().getQuery(); |
| 104 | + if (query != null) { // BH URL.getQuery() is supposed to return null. |
| 105 | + String[] fields = query.split("[\\=\\&#]"); |
| 106 | + for (int i = 0; i < fields.length; i += 2) |
| 107 | + if (fields[i].equals(key)) |
| 108 | + return fields[i + 1]; |
| 109 | + } |
| 110 | + return getParameter(key); |
| 111 | + } |
| 112 | + |
| 113 | + public void actionPerformed(ActionEvent e) { |
| 114 | + String cmd = e.getActionCommand(); |
| 115 | + if (cmd.equalsIgnoreCase("linked net")) { |
| 116 | + try { |
| 117 | + showMainPanel(new NetBuilder().decode(encodedNet), name); |
| 118 | + } catch (NullPointerException ex) { |
| 119 | + // BH NetBuilder.decode can throw NullPointerException() for |
| 120 | + // invalid encodedNet |
| 121 | + ex.printStackTrace(); |
| 122 | + } |
| 123 | + } else if (cmd.equalsIgnoreCase("new net")) { |
| 124 | + showMainPanel(null, null); |
| 125 | + } else if (cmd.equalsIgnoreCase("load net")) { |
| 126 | + PolyhedronFileFilter filter = new PolyhedronFileFilter(); |
| 127 | + |
| 128 | + final JSCallback cb = new JSCallback(); |
| 129 | + cb.setRunnable(new Runnable() { |
| 130 | + public void run() { |
| 131 | + if (cb.getData() instanceof File) { |
| 132 | + //first time, dialog returns the File object with __bytes attached |
| 133 | + try { |
| 134 | + FileUtils.loadNameAndNet((File) cb.getData(), cb); |
| 135 | + } catch (Exception e) { |
| 136 | + JOptionPane.showMessageDialog(PolyhedronApplet.this, "I couldn't read a polyhedron from your file!", "Error!", |
| 137 | + JOptionPane.ERROR_MESSAGE); |
| 138 | + } |
| 139 | + } else { |
| 140 | + // second time, returns the NameAndNet |
| 141 | + showMainPanel((NameAndNet) cb.getData()); |
| 142 | + } |
| 143 | + } |
| 144 | + }); |
| 145 | + File file = FileUtils.getFile(filter, cb, false); // BH added asynchronous file load option |
| 146 | + if (file != null) { |
| 147 | + try { |
| 148 | + // Java only |
| 149 | + FileUtils.loadNameAndNet(file, cb); // adding callback |
| 150 | + } catch (Exception e1) { |
| 151 | + e1.printStackTrace(); |
| 152 | + } |
| 153 | + if (!cb.getSuccessful()) { |
| 154 | + JOptionPane.showMessageDialog(this, "I couldn't read a polyhedron from your file!", "Error!", |
| 155 | + JOptionPane.ERROR_MESSAGE); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private void addButton(JPanel panel, String text, String tooltip) { |
| 162 | + JButton jb = new JButton(text); |
| 163 | + jb.setToolTipText(tooltip); |
| 164 | + jb.setActionCommand(text); |
| 165 | + panel.add(jb); |
| 166 | + jb.addActionListener(this); |
| 167 | + } |
| 168 | + |
| 169 | + private void showMainPanel(NameAndNet nn) { |
| 170 | + showMainPanel(nn.getNet(), nn.getName()); |
| 171 | + } |
| 172 | + private void showMainPanel(Net net, String name) { |
| 173 | + Color bgColor = Color.black; |
| 174 | + Color unjoinedEdgeColor = Color.red; |
| 175 | + Color joinedEdgeColor = Color.cyan; |
| 176 | + MarkColors markColors = new MarkColors(Color.green, Color.yellow, Color.blue); |
| 177 | + show(new MainPanel(joinedEdgeColor, bgColor, unjoinedEdgeColor, markColors,net, name), name); |
| 178 | + } |
| 179 | + |
| 180 | + private static void show(JPanel panel, String title) { |
| 181 | + if (title == null) title = "Polyhedral Nets"; |
| 182 | + JFrame jf = new JFrame(title); |
| 183 | + panel.setPreferredSize(new Dimension(800, 600)); |
| 184 | + jf.setContentPane(panel); |
| 185 | + jf.pack(); |
| 186 | + jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 187 | + jf.setVisible(true); |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments