📄 desktopappmanager.java
字号:
/* * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is * described in this document. In particular, and without limitation, these intellectual property rights may * include one or more of the U.S. patents listed at http://www.sun.com/patents and one or more additional patents * or pending patent applications in the U.S. and in other countries. * * U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. * standard license agreement and applicable provisions of the FAR and its supplements. * * Use is subject to license terms. * * This distribution may include materials developed by third parties. Sun, Sun Microsystems, the Sun logo and * Java are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. * * Copyright (c) 2006 Sun Microsystems, Inc. Tous droits r?serv?s. * * Sun Microsystems, Inc. d?tient les droits de propri?t? intellectuels relatifs ? la technologie incorpor?e dans * le produit qui est d?crit dans ce document. En particulier, et ce sans limitation, ces droits de propri?t? * intellectuelle peuvent inclure un ou plus des brevets am?ricains list?s ? l'adresse http://www.sun.com/patents * et un ou les brevets suppl?mentaires ou les applications de brevet en attente aux Etats - Unis et dans les * autres pays. * * L'utilisation est soumise aux termes du contrat de licence. * * Cette distribution peut comprendre des composants d?velopp?s par des tierces parties. * Sun, Sun Microsystems, le logo Sun et Java sont des marques de fabrique ou des marques d?pos?es de Sun * Microsystems, Inc. aux Etats-Unis et dans d'autres pays. */package com.sun.spot.spotworld.gui;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Map;import javax.swing.JFileChooser;import javax.swing.JMenuBar;import javax.swing.JMenuItem;/** * * @author randy */public class DesktopAppManager { public DesktopAppManager(){}; private static DesktopAppManager soleInstance; public static DesktopAppManager getInstance(){ if(soleInstance == null) soleInstance = new DesktopAppManager(); return soleInstance; } SpotWorldPortal swp; public void setSPOTWorldPortal(SpotWorldPortal s){ swp = s; } public String getCurrentDesktopProjDir() { String x = swp.getSPOTWorldProperty("DesktopAppDir"); if(x == null) x = "."; return x; } public void setCurrentDesktopProjDir(String x) { addRecentDesktopApp(x); swp.setSPOTWorldProperty("DesktopAppDir", x); } /* returns a comma-separated list of file names */ public String[] getRecentDesktopApps(){ String s = swp.getSPOTWorldProperty("recentDesktopAppDirs"); if (s==null) return new String[0]; return s.split(","); } /* Keep track of at most 5. The parameter app is a full file name */ public void addRecentDesktopApp(String app){ int maxApps = 5; String[] oldApps = getRecentDesktopApps(); //Return if app is already in here for (int i = 0; i < oldApps.length; i++) { if(oldApps[i].equals(app)) return; } // newApps array is one longer than the oldApps array. Up to a length of maxApps. String [] newApps = new String[Math.min(oldApps.length + 1, maxApps)]; if (oldApps.length >=maxApps) { // oldApps and newApps lengths are maxed out. // Remove oldApps[0] as the oldest, and move all others forward one. for(int i = 1; i < oldApps.length; i++){ newApps[i-1] = oldApps[i]; } } else { for(int i = 0; i < oldApps.length; i++){ newApps[i] = oldApps[i]; } } //The new app goes at the end of the list. newApps[newApps.length - 1] = app; // Make a single comma-separated string for storing as a property String newAppString = newApps[0]; for (int i = 1; i < newApps.length; i++) { newAppString = newApps[i] + "," + newAppString; } swp.setSPOTWorldProperty("recentDesktopAppDirs", newAppString); //Update the menu. JMenuBar mb = swp.createMenuBar(); swp.setJMenuBar(mb); //The next line is a Java Swing bug workaround. mb.revalidate(); } public void runAppFromUser(){ File appDir = getDesktopAppDirectoryFromUser("Select directory containting build.xml"); if(appDir == null) return; //User cancelled. runApp(appDir); } public void runApp(final File appDir){ final String[] cmd = {"ant", "host-run"}; Runnable r = new Runnable(){ public void run(){ try { Process proc = Runtime.getRuntime().exec( cmd, new String[0], appDir); // put a BufferedReader on the output BufferedReader bufferedreader = getBufferedReader(proc); // read the output String line; while ((line = bufferedreader.readLine()) != null) { System.out.println("[DesktopApp] " + line); } // check for failure try { if (proc.waitFor() != 0) { System.err.println("Desktop app abnormal exit, value = " + proc.exitValue()); } } catch (InterruptedException e) { System.err.println(e); } } catch (IOException ex) { ex.printStackTrace(); } } }; (new Thread(r)).start(); } File getDesktopAppDirectoryFromUser(String title){ AppProjectFileChooser fc; try { String dirName = getCurrentDesktopProjDir(); File dir= new File(dirName); String initialDirName = dir.getParent(); fc = new AppProjectFileChooser(initialDirName); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fc.setApproveButtonText("Use this directory"); fc.setDialogTitle(title); if(!dirName.endsWith(File.separator)) dirName = dirName + File.separator; File f = new File(dirName + "build.xml"); // On the Mac the next line does not work .. it is a known and filed bug. if(f.exists()) fc.setSelectedFile(f); int returnVal = fc.showOpenDialog(swp); if (returnVal == JFileChooser.APPROVE_OPTION) { File sel = fc.getSelectedFile(); //RECORD THIS ONE FOR POSTERITY setCurrentDesktopProjDir(sel.getCanonicalPath()); return sel; } } catch (IOException ex) { ex.printStackTrace(); } return null; } /* * Utility: Why doesn't Java just give us this stuff directly??? */ public BufferedReader getBufferedReader(Process proc){ InputStream inputstream = proc.getInputStream(); InputStreamReader inputstreamreader = new InputStreamReader(inputstream); BufferedReader bufferedreader = new BufferedReader(inputstreamreader); return bufferedreader ; } /* Creates a special kind of menu item that contains the full path name */ public DesktopAppMenuItem createMenuItem(String label, String appDirName){ DesktopAppMenuItem d = new DesktopAppMenuItem(label); d.appDirName = appDirName; return d; } /* * Do nothing if this is an everyday JMenuItem and return false. But if it is our special * kind of menu item, run the app named within it. Then return true. */ public boolean doMenuItem(JMenuItem mi){ if(mi instanceof DesktopAppMenuItem){ File appDir = new File(((DesktopAppMenuItem)mi).appDirName); runApp(appDir); return true; } else { return false; } }}/* * A special MenuItem for DesktopAppManager use that contains a full directory name. */class DesktopAppMenuItem extends JMenuItem { public DesktopAppMenuItem(String label){ super(label); } public String appDirName;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -