KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > uihandler > ProjectOp


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
16  */

17
18 package org.netbeans.lib.uihandler;
19
20 import java.util.logging.LogRecord JavaDoc;
21
22 /** Represents an operation on the list of opened projects.
23  *
24  * @author Jaroslav Tulach
25  * @since 1.7
26  */

27 public final class ProjectOp {
28     private final String JavaDoc name;
29     private final String JavaDoc type;
30     private final int number;
31
32     private ProjectOp(String JavaDoc name, String JavaDoc type, int number) {
33         this.name = fixName(name, true);
34         this.type = fixName(type, false);
35         this.number = number;
36     }
37     
38     private static String JavaDoc fixName(String JavaDoc name, boolean nameOrType) {
39         if (nameOrType && name.indexOf("Maven") >= 0) {
40             return "Maven";
41         }
42         return name;
43     }
44     
45     /** Human readable name of the project the operation happened on
46      */

47     public String JavaDoc getProjectDisplayName() {
48         return name;
49     }
50
51     /** Fully qualified class name of the project.
52      */

53     public String JavaDoc getProjectType() {
54         return type;
55     }
56     
57     /** Number of projects of this type that has been added.
58      * @return positive value if some projects were open, negative if some were closed
59      */

60     public int getDelta() {
61         return number;
62     }
63     
64     /** Finds whether the record was an operation on projects.
65      * @param rec the record to test
66      * @return null if the record is of unknown format or data about the project operation
67      */

68     public static ProjectOp valueOf(LogRecord JavaDoc rec) {
69         if ("UI_CLOSED_PROJECTS".equals(rec.getMessage())) {
70             String JavaDoc type = getStringParam(rec, 0, "unknown"); // NOI18N
71
String JavaDoc name = getStringParam(rec, 1, "unknown"); // NOI18N
72
int cnt = Integer.parseInt(getStringParam(rec, 2, "0"));
73             return new ProjectOp(name, type, -cnt);
74         }
75         if ("UI_OPEN_PROJECTS".equals(rec.getMessage())) {
76             String JavaDoc type = getStringParam(rec, 0, "unknown"); // NOI18N
77
String JavaDoc name = getStringParam(rec, 1, "unknown"); // NOI18N
78
int cnt = Integer.parseInt(getStringParam(rec, 2, "0"));
79             return new ProjectOp(name, type, cnt);
80         }
81         return null;
82     }
83     
84     private static String JavaDoc getStringParam(LogRecord JavaDoc rec, int index, String JavaDoc def) {
85         if (rec == null) {
86             return def;
87         }
88         Object JavaDoc[] params = rec.getParameters();
89         if (params == null || params.length <= index) {
90             return def;
91         }
92         if (params[index] instanceof String JavaDoc) {
93             return (String JavaDoc)params[index];
94         }
95         return def;
96     }
97 }
98
Popular Tags