1 /*******************************************************************************
2 * Copyright 2011 See AUTHORS file.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 package aurelienribon.gdxsetupui.ui.panels;
18 import aurelienribon.gdxsetupui.LibraryDef;
19 import aurelienribon.gdxsetupui.ui.Ctx;
20 import aurelienribon.ui.css.Style;
21 import aurelienribon.utils.Res;
22 import java.awt.BorderLayout;
23 import java.awt.Component;
24 import java.io.IOException;
26 import java.util.TreeMap;
27 import java.util.zip.ZipEntry;
28 import java.util.zip.ZipInputStream;
29 import javax.swing.JLabel;
30 import javax.swing.JScrollPane;
31 import javax.swing.JTree;
32 import javax.swing.border.EmptyBorder;
33 import javax.swing.tree.DefaultMutableTreeNode;
34 import javax.swing.tree.DefaultTreeModel;
35 import javax.swing.tree.TreeCellRenderer;
36 import org.apache.commons.io.FilenameUtils;
39 * @author Aurelien Ribon | http://www.aurelienribon.com/
41 public class PreviewPanel extends javax.swing.JPanel {
42 public PreviewPanel() {
44 Style.registerCssClasses(headerPanel, ".header");
45 Style.registerCssClasses(numberLabel, ".headerNumber");
47 JTree tree = new ResultTree();
48 JScrollPane scrollPane = new JScrollPane(tree);
49 scrollPane.setOpaque(false);
50 scrollPane.getViewport().setOpaque(false);
51 add(scrollPane, BorderLayout.CENTER);
54 // -------------------------------------------------------------------------
56 // -------------------------------------------------------------------------
58 public class ResultTree extends JTree {
59 private final Map<String, DefaultMutableTreeNode> nodes = new TreeMap<String, DefaultMutableTreeNode>();
60 private final DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();
63 setBorder(new EmptyBorder(5, 5, 5, 5));
64 setRootVisible(false);
65 setShowsRootHandles(true);
66 setCellRenderer(treeCellRenderer);
69 Ctx.listeners.add(new Ctx.Listener() {
70 @Override public void cfgSetupChanged() {
79 private void build() {
81 ZipInputStream zis = new ZipInputStream(Res.getStream("projects.zip"));
84 while ((entry = zis.getNextEntry()) != null) {
85 String name = entry.getName();
86 name = entry.isDirectory() ? "#DIR#" + name : name; // this makes name sorting easier :p
87 name = entry.isDirectory() ? name.substring(0, name.length()-1) : name;
89 DefaultMutableTreeNode node = new DefaultMutableTreeNode(name);
90 nodes.put(name, node);
95 for (String name : nodes.keySet()) {
96 String pName = name.startsWith("#DIR#") ? name : "#DIR#" + name;
97 pName = FilenameUtils.getPath(pName);
98 pName = pName.endsWith("/") ? pName.substring(0, pName.length()-1) : pName;
100 DefaultMutableTreeNode node = nodes.get(name);
101 DefaultMutableTreeNode pNode = nodes.get(pName);
103 if (pNode != null) pNode.add(node);
104 else rootNode.add(node);
107 } catch (IOException ex) {
112 private void update() {
113 DefaultMutableTreeNode commonPrjNode = nodes.get("#DIR#prj-common");
114 DefaultMutableTreeNode desktopPrjNode = nodes.get("#DIR#prj-desktop");
115 DefaultMutableTreeNode androidPrjNode = nodes.get("#DIR#prj-android");
116 DefaultMutableTreeNode htmlPrjNode = nodes.get("#DIR#prj-html");
117 DefaultMutableTreeNode iosPrjNode = nodes.get("#DIR#prj-ios");
118 DefaultMutableTreeNode robovmPrjNode = nodes.get("#DIR#prj-robovm");
120 rootNode.removeAllChildren();
121 rootNode.add(commonPrjNode);
122 if (Ctx.cfgSetup.isDesktopIncluded) rootNode.add(desktopPrjNode);
123 if (Ctx.cfgSetup.isAndroidIncluded) rootNode.add(androidPrjNode);
124 if (Ctx.cfgSetup.isHtmlIncluded) rootNode.add(htmlPrjNode);
125 if (Ctx.cfgSetup.isIosIncluded) {
126 rootNode.add(iosPrjNode);
127 rootNode.add(robovmPrjNode);
133 setModel(new DefaultTreeModel(rootNode));
136 private void updateSrc() {
137 DefaultMutableTreeNode previousNode;
141 DefaultMutableTreeNode commonSrcNode = nodes.get("#DIR#prj-common/src");
142 DefaultMutableTreeNode commonSrcAppNode = nodes.get("prj-common/src/MyGame.java");
143 DefaultMutableTreeNode commonSrcAppGwtNode = nodes.get("prj-common/src/MyGame.gwt.xml");
145 commonSrcNode.removeAllChildren();
146 previousNode = commonSrcNode;
148 if (!Ctx.cfgSetup.packageName.trim().equals("")) {
149 String[] paths = Ctx.cfgSetup.packageName.split("\\.");
150 for (String path : paths) {
151 DefaultMutableTreeNode node = new DefaultMutableTreeNode("#DIR#prj-common/src/" + path);
152 previousNode.add(node);
155 previousNode.add(commonSrcAppNode);
156 commonSrcNode.add(commonSrcAppGwtNode);
158 commonSrcNode.add(commonSrcAppNode);
159 commonSrcNode.add(commonSrcAppGwtNode);
164 DefaultMutableTreeNode desktopSrcNode = nodes.get("#DIR#prj-desktop/src");
165 DefaultMutableTreeNode desktopSrcMainNode = nodes.get("prj-desktop/src/Main.java");
167 desktopSrcNode.removeAllChildren();
168 previousNode = desktopSrcNode;
170 if (!Ctx.cfgSetup.packageName.trim().equals("")) {
171 String[] paths = Ctx.cfgSetup.packageName.split("\\.");
172 for (String path : paths) {
173 DefaultMutableTreeNode node = new DefaultMutableTreeNode("#DIR#prj-desktop/src/" + path);
174 previousNode.add(node);
177 previousNode.add(desktopSrcMainNode);
179 desktopSrcNode.add(desktopSrcMainNode);
184 DefaultMutableTreeNode androidSrcNode = nodes.get("#DIR#prj-android/src");
185 DefaultMutableTreeNode androidSrcActivityNode = nodes.get("prj-android/src/MainActivity.java");
187 androidSrcNode.removeAllChildren();
188 previousNode = androidSrcNode;
190 if (!Ctx.cfgSetup.packageName.trim().equals("")) {
191 String[] paths = Ctx.cfgSetup.packageName.split("\\.");
192 for (String path : paths) {
193 DefaultMutableTreeNode node = new DefaultMutableTreeNode("#DIR#prj-android/src/" + path);
194 previousNode.add(node);
197 previousNode.add(androidSrcActivityNode);
199 androidSrcNode.add(androidSrcActivityNode);
204 DefaultMutableTreeNode htmlSrcNode = nodes.get("#DIR#prj-html/src");
205 DefaultMutableTreeNode htmlSrcAppGwtNode = nodes.get("prj-html/src/GwtDefinition.gwt.xml");
206 DefaultMutableTreeNode htmlSrcClientDirNode = nodes.get("#DIR#prj-html/src/client");
208 htmlSrcNode.removeAllChildren();
209 previousNode = htmlSrcNode;
211 if (!Ctx.cfgSetup.packageName.trim().equals("")) {
212 String[] paths = Ctx.cfgSetup.packageName.split("\\.");
213 for (String path : paths) {
214 DefaultMutableTreeNode node = new DefaultMutableTreeNode("#DIR#prj-html/src/" + path);
215 previousNode.add(node);
218 previousNode.add(htmlSrcClientDirNode);
219 previousNode.add(htmlSrcAppGwtNode);
221 commonSrcNode.add(htmlSrcClientDirNode);
222 commonSrcNode.add(htmlSrcAppGwtNode);
226 private void updateLibs() {
227 DefaultMutableTreeNode commonLibsNode = nodes.get("#DIR#prj-common/libs");
228 DefaultMutableTreeNode desktopLibsNode = nodes.get("#DIR#prj-desktop/libs");
229 DefaultMutableTreeNode androidLibsNode = nodes.get("#DIR#prj-android/libs");
230 DefaultMutableTreeNode htmlLibsNode = nodes.get("#DIR#prj-html/war/WEB-INF/lib");
231 DefaultMutableTreeNode iosLibsNode = nodes.get("#DIR#prj-ios/libs");
232 DefaultMutableTreeNode robovmLibsNode = nodes.get("#DIR#prj-robovm/libs");
233 DefaultMutableTreeNode dataNode = nodes.get("#DIR#prj-android/assets");
235 commonLibsNode.removeAllChildren();
236 desktopLibsNode.removeAllChildren();
237 androidLibsNode.removeAllChildren();
238 htmlLibsNode.removeAllChildren();
239 iosLibsNode.removeAllChildren();
240 robovmLibsNode.removeAllChildren();
241 dataNode.removeAllChildren();
243 for (String libraryName : Ctx.cfgSetup.libraries) {
244 LibraryDef def = Ctx.libs.getDef(libraryName);
245 for (String path : def.libsCommon) pathToNodes(path, commonLibsNode);
246 for (String path : def.libsDesktop) pathToNodes(path, desktopLibsNode);
247 for (String path : def.libsAndroid) pathToNodes(path, androidLibsNode);
248 for (String path : def.libsHtml) pathToNodes(path, htmlLibsNode);
249 for (String path : def.libsIos) pathToNodes(path, iosLibsNode);
250 for (String path : def.libsRobovm) pathToNodes(path, robovmLibsNode);
251 for (String path : def.data) pathToNodes(path, dataNode);
255 private void pathToNodes(String path, DefaultMutableTreeNode parentNode) {
256 String parentPath = (String) parentNode.getUserObject();
257 String[] names = path.split("/");
259 for (int i=0; i<names.length; i++) {
260 if (i == 0) names[i] = parentPath + "/" + names[i];
261 else names[i] = names[i-1] + "/" + names[i];
263 if (i == names.length-1) names[i] = names[i].replaceFirst("#DIR#", "");
265 DefaultMutableTreeNode node = nodes.get(names[i]);
267 node = new DefaultMutableTreeNode(names[i]);
268 nodes.put(names[i], node);
269 if (i == 0) parentNode.add(node);
270 else nodes.get(names[i-1]).add(node);
272 parentNode.add(node);
277 private final TreeCellRenderer treeCellRenderer = new TreeCellRenderer() {
278 private final JLabel label = new JLabel();
281 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
282 DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
283 if (node.getUserObject() instanceof String) {
284 String name = (String) node.getUserObject();
285 boolean isDir = name.startsWith("#DIR#");
287 name = name.replaceFirst("#DIR#", "");
288 name = name.replace("MyGame", Ctx.cfgSetup.mainClassName);
290 if (isDir && name.equals("prj-common")) name = Ctx.cfgSetup.projectName + Ctx.cfgSetup.suffixCommon;
291 if (isDir && name.equals("prj-desktop")) name = Ctx.cfgSetup.projectName + Ctx.cfgSetup.suffixDesktop;
292 if (isDir && name.equals("prj-android")) name = Ctx.cfgSetup.projectName + Ctx.cfgSetup.suffixAndroid;
293 if (isDir && name.equals("prj-html")) name = Ctx.cfgSetup.projectName + Ctx.cfgSetup.suffixHtml;
294 if (isDir && name.equals("prj-ios")) name = Ctx.cfgSetup.projectName + Ctx.cfgSetup.suffixIos;
295 if (isDir && name.equals("prj-robovm")) name = Ctx.cfgSetup.projectName + Ctx.cfgSetup.suffixRobovm;
297 label.setText(FilenameUtils.getName(name));
298 label.setIcon(isDir ? Res.getImage("gfx/ic_folder.png") : Res.getImage("gfx/ic_file.png"));
306 // -------------------------------------------------------------------------
308 // -------------------------------------------------------------------------
310 @SuppressWarnings("unchecked")
311 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
312 private void initComponents() {
314 headerPanel = new javax.swing.JPanel();
315 jLabel4 = new javax.swing.JLabel();
316 numberLabel = new javax.swing.JLabel();
318 setLayout(new java.awt.BorderLayout());
320 jLabel4.setText("<html> Virtual view of the file tree that will be generated.");
321 jLabel4.setVerticalAlignment(javax.swing.SwingConstants.TOP);
323 numberLabel.setText("3");
325 javax.swing.GroupLayout headerPanelLayout = new javax.swing.GroupLayout(headerPanel);
326 headerPanel.setLayout(headerPanelLayout);
327 headerPanelLayout.setHorizontalGroup(
328 headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
329 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, headerPanelLayout.createSequentialGroup()
330 .addComponent(numberLabel)
331 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
332 .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE))
334 headerPanelLayout.setVerticalGroup(
335 headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
336 .addComponent(jLabel4)
337 .addComponent(numberLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
340 add(headerPanel, java.awt.BorderLayout.NORTH);
341 }// </editor-fold>//GEN-END:initComponents
343 // Variables declaration - do not modify//GEN-BEGIN:variables
344 private javax.swing.JPanel headerPanel;
345 private javax.swing.JLabel jLabel4;
346 private javax.swing.JLabel numberLabel;
347 // End of variables declaration//GEN-END:variables