KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > cheatsheet > CSAbstractAddAction


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.ui.editor.cheatsheet;
13
14 import java.util.HashSet JavaDoc;
15
16 import org.eclipse.jface.action.Action;
17
18 /**
19  * SimpleCSAbstractAdd
20  *
21  */

22 public abstract class CSAbstractAddAction extends Action {
23
24     /**
25      *
26      */

27     public CSAbstractAddAction() {
28     }
29
30     /**
31      * @param result
32      * @param set
33      */

34     protected void addNumberToBase(StringBuffer JavaDoc base, HashSet JavaDoc set) {
35         if (set.size() > 0) {
36             // Limit on the number of auto-generated item numbers to check for
37
int limit = 100;
38             // Check the set for the numbers encountered and generate the
39
// lowest number accordingly
40
if (set.contains(new Integer JavaDoc(0)) == false) {
41                 // Use base
42
} else {
43                 for (int x = 1; x < limit; x++) {
44                     // Check if the number was already used to auto-generate an
45
// existing item
46
if (set.contains(new Integer JavaDoc(x)) == false) {
47                         base.append(" ("); //$NON-NLS-1$
48
base.append(x);
49                         base.append(")"); //$NON-NLS-1$
50
break;
51                     }
52                 }
53             }
54         }
55     }
56
57     /**
58      * @param base
59      * @param set
60      * @param title
61      */

62     protected void compareTitleWithBase(String JavaDoc base, HashSet JavaDoc set, String JavaDoc title) {
63         // Check to see it the name starts with the base
64
if (title.startsWith(base)) {
65             // space, (, number, )
66
int minSizeNumAddOn = 4;
67             // We found a possible auto-generated name
68
// Determine number
69
if (title.length() >= (base.length() + minSizeNumAddOn)) {
70                 // We skipped the space
71
String JavaDoc numPart = title.substring(base.length() + 1);
72                 // We found an auto-generated name
73
if (numPart.charAt(0) == '(') {
74                     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
75                     // Parse the number between the brackets
76
for (int j = 1; j < numPart.length(); j++) {
77                         char current = numPart.charAt(j);
78                         // Make sure its a digit
79
if (Character.isDigit(current)) {
80                             buffer.append(current);
81                         } else {
82                             // Break on non digits including ')'
83
break;
84                         }
85                     }
86                     // Convert the number we found into an actual number
87
if (buffer.length() > 0) {
88                         set.add(new Integer JavaDoc(buffer.toString()));
89                     }
90                 }
91                 
92             } else {
93                 // No number to parse
94
// Assume it is just base
95
set.add(new Integer JavaDoc(0));
96             }
97         }
98     }
99
100 }
101
Popular Tags