UserInputHistory.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.common.util;
00002 
00003 import java.io.UnsupportedEncodingException;
00004 import java.util.ArrayList;
00005 import java.util.Arrays;
00006 import java.util.Iterator;
00007 import java.util.List;
00008 
00009 import org.eclipse.core.runtime.preferences.ConfigurationScope;
00010 import org.osgi.service.prefs.BackingStoreException;
00011 import org.osgi.service.prefs.Preferences;
00012 
00013 
00014 /*****
00015  * 
00016  * class to handle the history of user inputs (such as metric names and formula)
00017  * the class will store the data into Eclipse's workspace which should be
00018  * writable to each user
00019  *
00020  */
00021 public class UserInputHistory {
00022     private static final String HISTORY_NAME_BASE = "history."; //$NON-NLS-1$
00023     private final static String ENCODING = "UTF-8";
00024     
00025     // temporary revert back to use deprecated code in order to keep backward compatibility
00026     // Original code:
00027     //     private static final Preferences CONFIGURATION = ConfigurationScope.INSTANCE.getNode("edu.rice.cs.hpc");   
00028     //private static final Preferences CONFIGURATION = new ConfigurationScope().getNode("edu.rice.cs.hpc");
00029     final static String NODE_HPC = "edu.rice.cs.hpc";
00030     
00031     private static final Preferences CONFIGURATION = ConfigurationScope.INSTANCE.getNode(NODE_HPC);
00032     
00033     private String name;
00034     private int depth;
00035     private List<String> history;
00036 
00037 
00038     public UserInputHistory(String name) {
00039         this(name, 50);
00040     }
00041 
00042     public UserInputHistory(String name, int depth) {
00043         this.name = name;
00044         this.depth = depth;
00045         
00046         this.loadHistoryLines();
00047     }
00048     
00049     public String getName() {
00050         return this.name;
00051     }
00052     
00053     public int getDepth() {
00054         return this.depth;
00055     }
00056     
00057     public String []getHistory() {
00058         return (String [])this.history.toArray(new String[this.history.size()]);
00059     }
00060     
00061     public void addLine(String line) {
00062         if (line == null || line.trim().length() == 0) {
00063             return;
00064         }
00065         this.history.remove(line);
00066         this.history.add(0, line);
00067         if (this.history.size() > this.depth) {
00068             this.history.remove(this.history.size() - 1);
00069         }
00070         this.saveHistoryLines();
00071     }
00072     
00073     public void clear() {
00074         this.history.clear();
00075         this.saveHistoryLines();
00076     }
00077 
00078     /****
00079      * retrieve the preference of this application
00080      * @param node
00081      * @return
00082      */
00083     static public Preferences getPreference(String node) {
00084         return CONFIGURATION.node(node);
00085     }
00086     
00087     /****
00088      * force to store a preference
00089      * @param pref
00090      */
00091     static public void setPreference( Preferences pref ) {
00092         // Forces the application to save the preferences
00093         try {
00094             pref.flush();
00095         } catch (BackingStoreException e) {
00096             e.printStackTrace();
00097         }
00098 
00099     }
00100     
00101     protected void loadHistoryLines() {
00102         this.history = new ArrayList<String>();
00103         String historyData = getPreference(HISTORY_NAME_BASE).get(this.name, ""); 
00104 
00105         if (historyData != null && historyData.length() > 0) {
00106             String []historyArray = historyData.split(";"); //$NON-NLS-1$
00107             for (int i = 0; i < historyArray.length; i++) {
00108                 try {
00109                     historyArray[i] = new String(historyArray[i].getBytes(UserInputHistory.ENCODING), UserInputHistory.ENCODING);
00110                 } catch (UnsupportedEncodingException e) {
00111                     historyArray[i] = new String(historyArray[i].getBytes());
00112                 }
00113             }
00114             this.history.addAll(Arrays.asList(historyArray));
00115         }
00116     }
00117     
00118     protected void saveHistoryLines() {
00119         String result = ""; //$NON-NLS-1$
00120         for (Iterator<String> it = this.history.iterator(); it.hasNext(); ) {
00121             String str = (String)it.next();
00122             try {
00123                 str = new String(str.getBytes(UserInputHistory.ENCODING), UserInputHistory.ENCODING);
00124             } catch (UnsupportedEncodingException e) {
00125                 str = new String(str.getBytes());
00126             }
00127             result += result.length() == 0 ? str : (";" + str); //$NON-NLS-1$
00128         }
00129         Preferences pref = getPreference(HISTORY_NAME_BASE);
00130         pref.put(this.name, result);
00131         setPreference( pref );
00132     }
00133 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1