001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * Created on Oct 8, 2003
044: *
045: */
046: package org.netbeans.modules.uml.core.support.umlmessagingcore;
047:
048: import java.io.File;
049: import java.util.Iterator;
050: import java.util.List;
051:
052: import org.dom4j.Document;
053: import org.dom4j.Element;
054: import org.dom4j.Node;
055:
056: import org.netbeans.modules.uml.core.coreapplication.ICoreProduct;
057: import org.netbeans.modules.uml.core.metamodel.core.foundation.IConfigManager;
058: import org.netbeans.modules.uml.core.support.umlsupport.ProductRetriever;
059: import org.netbeans.modules.uml.core.support.umlsupport.XMLManip;
060: import org.netbeans.modules.uml.core.support.umlutils.ETArrayList;
061: import org.netbeans.modules.uml.core.support.umlutils.ETList;
062:
063: /**
064: * @author aztec
065: *
066: */
067: public class MessageDataFilter implements IMessageDataFilter {
068:
069: private IMessageService m_MessageService;
070: private ETList<IMessageFacilityFilter> m_Filters;
071: private String m_FileLocation;
072:
073: public MessageDataFilter() {
074: m_Filters = new ETArrayList<IMessageFacilityFilter>();
075: }
076:
077: /**
078: * Initializes the class by providing the xml file to store preferences in and
079: * the messenger which this filter applies to. Eventually we need to combine the
080: * facilities found in the messenger with those in the xml file - because the messenger
081: * only has a list of what facilities have sent messages for this run, whereas the file
082: * will have messages for the last run.
083: *
084: * @param fileLocation[in] The location of the message filter preference file
085: * @param pMessenger[in] The messenger this filter applies to
086: *
087: * @todo To get all past filters I think I need to reread the file in initialize
088: */
089: public void initialize(String fileLocation,
090: IMessageService pMessenger) {
091: m_FileLocation = fileLocation;
092: m_MessageService = pMessenger;
093:
094: try {
095: // Make sure the file location is absolute. If it isn't then go
096: // off of our home location
097: File file = new File(m_FileLocation);
098: if (file != null) {
099: if (!file.isAbsolute()) {
100: // Get the home location from the config manager
101: ICoreProduct pCoreProduct = ProductRetriever
102: .retrieveProduct();
103: if (pCoreProduct != null) {
104: IConfigManager configMan = pCoreProduct
105: .getConfigManager();
106: if (configMan != null) {
107: String homeLoc = configMan
108: .getHomeLocation();
109: if (homeLoc != null && homeLoc.length() > 0) {
110: String tempLoc = new File(homeLoc,
111: m_FileLocation).toString();
112: m_FileLocation = tempLoc;
113: }
114: }
115: }
116: }
117: reRead();
118: save();
119: }
120: } catch (Exception e) {
121: e.printStackTrace();
122: }
123: }
124:
125: /**
126: * Returns a list of facility filters. Each item in the list corresponds to a
127: * particular facility and indicates what message types should be displayed.
128: *
129: * @return The list of filters - one item for each facility
130: */
131: public ETList<IMessageFacilityFilter> getFilters() {
132: return m_Filters;
133: }
134:
135: /**
136: * Given a particular message this routine returns a true or false
137: * indicating if the filter settings would display this message.
138: *
139: * @param pMessageData[in] The message under question. Should this be displayed.
140: * @return whether or not the message should be displayed
141: */
142: public boolean getIsDisplayed(IMessageData pMessageData) {
143: boolean retVal = false;
144: try {
145: if (pMessageData != null) {
146: String sFacility = pMessageData.getFacility();
147: int nMessageType = pMessageData.getMessageType();
148:
149: retVal = getIsDisplayed(nMessageType, sFacility);
150: }
151: } catch (Exception e) {
152: e.printStackTrace();
153: }
154: return retVal;
155: }
156:
157: /**
158: * Given a particular message this routine sets the facility and message type to
159: * be displayed according to the parameter newVal.
160: *
161: * @param pMessageData[in] The message under question. Should this be displayed
162: * @param newVal[in] Sets if it should be displayed
163: */
164: public void setIsDisplayed(IMessageData pMessageData, boolean newVal) {
165: try {
166: if (pMessageData != null) {
167: String sFacility = pMessageData.getFacility();
168: int nMessageType = pMessageData.getMessageType();
169:
170: setIsDisplayed(nMessageType, sFacility, newVal);
171: }
172: } catch (Exception e) {
173: e.printStackTrace();
174: }
175: }
176:
177: /**
178: * Given a particular message type and facility, this routine returns a true or false
179: * indicating if the filter settings would display this message.
180: *
181: * @param nMessageType[in] The message type under question. Should this be displayed
182: * @param sFacility[in] The message type facility
183: * @return whether or not the message should be displayed
184: */
185: public boolean getIsDisplayed(int nMessageType, String sFacility) {
186: boolean retVal;
187: IMessageFacilityFilter msgFilter = getFilter(sFacility);
188: if (msgFilter != null) {
189: retVal = msgFilter.getIsDisplayed(nMessageType);
190: } else {
191: msgFilter = new MessageFacilityFilter();
192: msgFilter.setName(sFacility);
193: m_Filters.add(msgFilter);
194:
195: retVal = true;
196: }
197: return retVal;
198: }
199:
200: /**
201: * Gets the filter corresponding to the argument facility name.
202: *
203: * @param sFacility The facility we want to search for
204: * @param pFilter The output filter
205: */
206: protected IMessageFacilityFilter getFilter(String sFacility) {
207: IMessageFacilityFilter retFilter = null;
208: if (m_Filters != null && sFacility != null) {
209: Iterator<IMessageFacilityFilter> iter = m_Filters
210: .iterator();
211: if (iter != null) {
212: while (iter.hasNext()) {
213: IMessageFacilityFilter msgFacilityItem = iter
214: .next();
215: if (msgFacilityItem != null) {
216: if (sFacility.equals(msgFacilityItem.getName())) {
217: retFilter = msgFacilityItem;
218: break;
219: }
220: }
221: }
222:
223: }
224: }
225: return retFilter;
226: }
227:
228: /**
229: * Given a particular message type and facilty, this routine sets the facility and message type to
230: * be displayed according to the parameter newVal.
231: *
232: * @param nMessageType[in] The message type under question. Should this be displayed
233: * @param sFacility[in] The message type facility
234: * @param newVal[in] Sets if it should be displayed
235: */
236: public void setIsDisplayed(int nMessageType, String sFacility,
237: boolean newValue) {
238: IMessageFacilityFilter msgFilter = getFilter(sFacility);
239: if (msgFilter != null) {
240: msgFilter.setIsDisplayed(nMessageType, newValue);
241: } else {
242: msgFilter = new MessageFacilityFilter();
243: msgFilter.setName(sFacility);
244: msgFilter.setIsDisplayed(nMessageType, newValue);
245: m_Filters.add(msgFilter);
246: }
247: }
248:
249: /**
250: * Saves the file.
251: *
252: * @see CMessageDataFilter::Initialize
253: */
254: public void save() {
255: Document pDoc = XMLManip.getDOMDocument();
256: Element pCreatedDOMElement = XMLManip.createElement(pDoc,
257: "FACILITYFILTERS");
258: Iterator<IMessageFacilityFilter> iter = m_Filters.iterator();
259: if (iter != null && pCreatedDOMElement != null) {
260: while (iter.hasNext()) {
261: IMessageFacilityFilter pFilterItem = iter.next();
262: if (pFilterItem != null) {
263: Element pCreatedFacilityElement = XMLManip
264: .createElement(pCreatedDOMElement,
265: "FACILITY");
266: if (pCreatedFacilityElement != null) {
267: //Add the facility Name
268: XMLManip.setAttributeValue(
269: pCreatedFacilityElement, "NAME",
270: pFilterItem.getName());
271:
272: // Add the Facility Display flags
273: XMLManip
274: .setAttributeValue(
275: pCreatedFacilityElement,
276: "MT_CRITICAL",
277: Boolean
278: .toString(pFilterItem
279: .getIsDisplayed(MsgCoreConstants.MT_CRITICAL)));
280: XMLManip
281: .setAttributeValue(
282: pCreatedFacilityElement,
283: "MT_ERROR",
284: Boolean
285: .toString(pFilterItem
286: .getIsDisplayed(MsgCoreConstants.MT_ERROR)));
287: XMLManip
288: .setAttributeValue(
289: pCreatedFacilityElement,
290: "MT_WARNING",
291: Boolean
292: .toString(pFilterItem
293: .getIsDisplayed(MsgCoreConstants.MT_WARNING)));
294: XMLManip
295: .setAttributeValue(
296: pCreatedFacilityElement,
297: "MT_INFO",
298: Boolean
299: .toString(pFilterItem
300: .getIsDisplayed(MsgCoreConstants.MT_INFO)));
301: XMLManip
302: .setAttributeValue(
303: pCreatedFacilityElement,
304: "MT_DEBUG",
305: Boolean
306: .toString(pFilterItem
307: .getIsDisplayed(MsgCoreConstants.MT_DEBUG)));
308: }
309: }
310: }
311: }
312: XMLManip.save(pDoc, m_FileLocation);
313: }
314:
315: /**
316: * Rereads the file from disk.
317: *
318: * @see CMessageDataFilter::Initialize
319: */
320: public void reRead() {
321: Document pDoc = XMLManip.getDOMDocument(m_FileLocation);
322: if (pDoc != null) {
323: Node pFilterElements = XMLManip.selectSingleNode(pDoc,
324: "FACILITYFILTERS");
325: if (pFilterElements != null) {
326: int count = 0;
327: List pFiltersNodeList = XMLManip.selectNodeList(
328: pFilterElements, "FACILITY");
329: if (pFiltersNodeList != null
330: && (count = pFiltersNodeList.size()) > 0) {
331: Node pFacilityNode = null;
332: for (int i = 0; i < count; i++) {
333: pFacilityNode = (Node) pFiltersNodeList.get(i);
334: if (pFacilityNode != null) {
335: IMessageFacilityFilter pFilter = new MessageFacilityFilter();
336: Node node;
337: // Name
338: pFilter
339: .setName((node = XMLManip
340: .getAttribute(
341: pFacilityNode,
342: "NAME")) != null ? node
343: .getText()
344: : null);
345:
346: // Facility Display flags
347: boolean flag = node != null ? Boolean
348: .valueOf(node.getText())
349: .booleanValue() : false;
350:
351: pFilter
352: .setIsDisplayed(
353: MsgCoreConstants.MT_CRITICAL,
354: (node = XMLManip
355: .getAttribute(
356: pFacilityNode,
357: "MT_CRITICAL")) != null ? flag
358: : false);
359: pFilter
360: .setIsDisplayed(
361: MsgCoreConstants.MT_ERROR,
362: (node = XMLManip
363: .getAttribute(
364: pFacilityNode,
365: "MT_ERROR")) != null ? flag
366: : false);
367: pFilter
368: .setIsDisplayed(
369: MsgCoreConstants.MT_WARNING,
370: (node = XMLManip
371: .getAttribute(
372: pFacilityNode,
373: "MT_WARNING")) != null ? flag
374: : false);
375: pFilter
376: .setIsDisplayed(
377: MsgCoreConstants.MT_INFO,
378: (node = XMLManip
379: .getAttribute(
380: pFacilityNode,
381: "MT_INFO")) != null ? flag
382: : false);
383: pFilter
384: .setIsDisplayed(
385: MsgCoreConstants.MT_DEBUG,
386: (node = XMLManip
387: .getAttribute(
388: pFacilityNode,
389: "MT_DEBUG")) != null ? flag
390: : false);
391:
392: m_Filters.add(pFilter);
393: }
394: }
395: }
396: }
397: }
398: }
399:
400: }
|