KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mullassery > act > gui > MRUManager


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "The Jakarta Project", "Ant", and "Apache Software
27  * Foundation" must not be used to endorse or promote products derived
28  * from this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation. For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  */

54
55 package com.mullassery.act.gui;
56
57 import javax.swing.JComponent JavaDoc;
58 import javax.swing.JMenu JavaDoc;
59 import javax.swing.JSeparator JavaDoc;
60
61 import org.w3c.dom.Document JavaDoc;
62 import org.w3c.dom.Element JavaDoc;
63 import org.w3c.dom.NodeList JavaDoc;
64
65 import com.mullassery.act.util.DebugUtil;
66 import com.mullassery.act.util.GUIUtil;
67 import com.mullassery.act.util.ResourceUtil;
68 import com.mullassery.act.util.XMLUtil;
69
70 /**
71  *
72  * @author Abey Mullassery
73  *
74  */

75 final class MRUManager {
76
77     public static final int MRU_LIST_MAX = 4;
78     private ACT act;
79     private Element JavaDoc mru;
80     private boolean changeMenu = true;
81     private boolean mruChanged = false;
82     private JComponent JavaDoc[] mruItems;
83     /**
84      *
85      */

86     public MRUManager(ACT act) {
87         this.act = act;
88         try {
89             mru = XMLUtil.loadDocument(ResourceUtil.getMRUFile(), "mru-tasks");
90         } catch (Exception JavaDoc e) {
91             //GUIUtil.showErrorMessage(this, "Error while opening MRU " + e);
92
}
93     }
94
95     private void importTask(Element JavaDoc vals, String JavaDoc sName) {
96         Document JavaDoc doc = mru.getOwnerDocument();
97         Element JavaDoc m = (Element JavaDoc) doc.importNode(vals, true);
98         m.setAttribute(GUIUtil.TASK_DISPLAY, sName);
99         addTask(m);
100     }
101
102     private void addTask(Element JavaDoc m) {
103         if (mru.hasChildNodes())
104             mru.insertBefore(m, mru.getFirstChild());
105         else
106             mru.appendChild(m);
107
108         NodeList JavaDoc nList = mru.getElementsByTagName("task");
109         while (nList.getLength() > MRU_LIST_MAX) {
110             //if list is more than 4 trim it
111
mru.removeChild(nList.item(nList.getLength() - 1));
112             nList = mru.getElementsByTagName("task");
113         }
114
115         changeMenu = true;
116         mruChanged = true;
117     }
118
119     public void addToMRU(TaskPanel tp) {
120         Element JavaDoc vals = tp.getTaskValues();
121         String JavaDoc displayName = vals.getAttribute(GUIUtil.TASK_DISPLAY);
122         NodeList JavaDoc nList = mru.getElementsByTagName(GUIUtil.TASK); //trimMRU();
123
int maxSuffix = 0;
124         boolean intTaken[] = new boolean[MRU_LIST_MAX+1];
125         //compare each to see if anyone matches
126
for (int i = 0; i < nList.getLength(); i++) {
127             Element JavaDoc tsk = (Element JavaDoc) nList.item(i);
128             if (tsk == vals) { // same task exits in the MRU, probably lower.
129
mru.removeChild(tsk);
130                 addTask(tsk);
131                 return;
132             }
133
134             String JavaDoc mName = tsk.getAttribute(GUIUtil.TASK_DISPLAY);
135             if (mName.startsWith(displayName)
136                 && mName.charAt(mName.length() - 2) == '_') {
137                 try {
138                     int temp =
139                         Integer.parseInt("" + mName.charAt(mName.length() - 1))
140                             - 1;
141                     if (temp >= 0 && temp < MRU_LIST_MAX)
142                         intTaken[temp] = true;
143                 } catch (Exception JavaDoc ex) {
144                     DebugUtil.debug("Error while reading number suffix");
145                 }
146             }
147         }
148         //TODO: change order to make last updated 1
149
for (int i = 0; i < intTaken.length; i++) {
150             if (!intTaken[i]) {
151                 maxSuffix = i;
152                 break;
153             }
154         }
155         importTask(vals, displayName + "_" + (maxSuffix + 1));
156         saveMRU(); //since this is the only method changing the XML
157
}
158
159     public void saveMRU() {
160         if (mru != null && mruChanged) {
161             try {
162                 XMLUtil.saveElement(mru, ResourceUtil.getMRUFile(), false);
163             } catch (Exception JavaDoc e) {
164                 //GUIUtil.showErrorMessage(this, "Error while saving.." + e);
165
}
166         }
167     }
168
169     /**
170      * create or update the MRU menu
171      */

172     public void updateMRU(JMenu JavaDoc fileMenu) {
173         //TODO: CHECK IF AN EXISTING ONE WAS CHAGED, IF SO JUST KEEP IT UPDATED; DONT ADD NEW
174
if (!changeMenu)
175             return;
176         if (mruItems != null && mruItems.length > 0) {
177             for (int i = 0; i < mruItems.length; i++) {
178                 fileMenu.remove(mruItems[i]);
179             }
180         }
181         if (mru == null || !mru.hasChildNodes())
182             return;
183
184         NodeList JavaDoc nl = mru.getElementsByTagName("task");
185         mruItems = new JComponent JavaDoc[nl.getLength() + 2];
186         mruItems[0] =
187             (JComponent JavaDoc) fileMenu.add(
188                 new JSeparator JavaDoc(),
189                 fileMenu.getItemCount() - 1);
190         for (int i = 0; i < nl.getLength(); i++) {
191             Element JavaDoc e = (Element JavaDoc) nl.item(i);
192             try {
193                 mruItems[i + 1] =
194                     fileMenu.insert(
195                         new OpenTaskAction(act, e, false),
196                         fileMenu.getItemCount() - 1);
197             } catch (Exception JavaDoc ex) {
198                 GUIUtil.showErrorMessage(
199                     act,
200                     "Failed task loading " + ex.getMessage());
201             }
202         }
203         mruItems[mruItems.length - 1] =
204             (JComponent JavaDoc) fileMenu.add(
205                 new JSeparator JavaDoc(),
206                 fileMenu.getItemCount() - 1);
207         changeMenu = false;
208     }
209 }
210
Popular Tags