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: * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
027: */
028:
029: package org.netbeans.lib.uihandler;
030:
031: import java.util.logging.LogRecord;
032:
033: /** Represents an operation on the list of opened projects.
034: *
035: * @author Jaroslav Tulach
036: * @since 1.7
037: */
038: public final class ProjectOp {
039: private final String name;
040: private final String type;
041: private final int number;
042:
043: private ProjectOp(String name, String type, int number) {
044: this .name = fixName(name, true);
045: this .type = fixName(type, false);
046: this .number = number;
047: }
048:
049: private static String fixName(String name, boolean nameOrType) {
050: if (nameOrType && name.indexOf("Maven") >= 0) {
051: return "Maven";
052: }
053: return name;
054: }
055:
056: /** Human readable name of the project the operation happened on
057: */
058: public String getProjectDisplayName() {
059: return name;
060: }
061:
062: /** Fully qualified class name of the project.
063: */
064: public String getProjectType() {
065: return type;
066: }
067:
068: /** Number of projects of this type that has been added.
069: * @return positive value if some projects were open, negative if some were closed
070: */
071: public int getDelta() {
072: return number;
073: }
074:
075: /** Finds whether the record was an operation on projects.
076: * @param rec the record to test
077: * @return null if the record is of unknown format or data about the project operation
078: */
079: public static ProjectOp valueOf(LogRecord rec) {
080: if ("UI_CLOSED_PROJECTS".equals(rec.getMessage())) {
081: String type = getStringParam(rec, 0, "unknown"); // NOI18N
082: String name = getStringParam(rec, 1, "unknown"); // NOI18N
083: int cnt;
084: try {
085: cnt = Integer.parseInt(getStringParam(rec, 2, "0"));
086: } catch (NumberFormatException numberFormatException) {
087: return null;
088: }
089: return new ProjectOp(name, type, -cnt);
090: }
091: if ("UI_OPEN_PROJECTS".equals(rec.getMessage())) {
092: String type = getStringParam(rec, 0, "unknown"); // NOI18N
093: String name = getStringParam(rec, 1, "unknown"); // NOI18N
094: int cnt;
095: try {
096: cnt = Integer.parseInt(getStringParam(rec, 2, "0"));
097: } catch (NumberFormatException numberFormatException) {
098: return null;
099: }
100: return new ProjectOp(name, type, cnt);
101: }
102: return null;
103: }
104:
105: private static String getStringParam(LogRecord rec, int index,
106: String def) {
107: if (rec == null) {
108: return def;
109: }
110: Object[] params = rec.getParameters();
111: if (params == null || params.length <= index) {
112: return def;
113: }
114: if (params[index] instanceof String) {
115: return (String) params[index];
116: }
117: return def;
118: }
119: }
|