KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > cheatsheet > simple > actions > SimpleCSAddStepAction


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.simple.actions;
13
14 import java.util.HashSet JavaDoc;
15
16 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS;
17 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConstants;
18 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSDescription;
19 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSIntro;
20 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem;
21 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory;
22 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject;
23 import org.eclipse.pde.internal.ui.PDEUIMessages;
24 import org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractAddAction;
25 import org.eclipse.pde.internal.ui.wizards.cheatsheet.BaseCSCreationOperation;
26
27 /**
28  * SimpleCSAddStepAction
29  *
30  */

31 public class SimpleCSAddStepAction extends CSAbstractAddAction {
32
33     private ISimpleCS fCheatsheet;
34     
35     private ISimpleCSItem fItem;
36     
37     private ISimpleCSIntro fIntro;
38     
39     /**
40      *
41      */

42     public SimpleCSAddStepAction() {
43         setText(PDEUIMessages.SimpleCSAddStepAction_0);
44     }
45
46     /**
47      * @param csObject
48      */

49     public void setDataObject(ISimpleCSObject csObject) {
50         // Determine input
51
if (csObject.getType() == ISimpleCSConstants.TYPE_CHEAT_SHEET) {
52             fIntro = null;
53             fItem = null;
54             fCheatsheet = (ISimpleCS)csObject;
55         } else if (csObject.getType() == ISimpleCSConstants.TYPE_ITEM) {
56             fIntro = null;
57             fItem = (ISimpleCSItem)csObject;
58             fCheatsheet = fItem.getSimpleCS();
59         } else if (csObject.getType() == ISimpleCSConstants.TYPE_INTRO) {
60             fIntro = (ISimpleCSIntro)csObject;
61             fItem = null;
62             fCheatsheet = fIntro.getSimpleCS();
63         } else {
64             // Invalid input, action will not run
65
fIntro = null;
66             fItem = null;
67             fCheatsheet = null;
68         }
69     }
70
71     /* (non-Javadoc)
72      * @see org.eclipse.jface.action.Action#run()
73      */

74     public void run() {
75         // Ensure we have valid input
76
if (fCheatsheet == null) {
77             return;
78         }
79         // Create the new item
80
ISimpleCSItem newItem = createNewItem();
81         // Insert the new item
82
insertNewItem(newItem);
83     }
84
85     /**
86      * @param item
87      */

88     private void insertNewItem(ISimpleCSItem newItem) {
89         // Insert the new item depending on the input specfied
90
if (fIntro != null) {
91             // Intro input object
92
// Insert item as the first cheat sheet child item which is the first
93
// node after the introduction node
94
fCheatsheet.addItem(0, newItem);
95         } else if (fItem != null) {
96             // Item input object
97
// Insert item right after the input item object
98
int index = fCheatsheet.indexOfItem(fItem) + 1;
99             fCheatsheet.addItem(index, newItem);
100         } else {
101             // Cheat sheet input object
102
// Add item as the last cheat sheet child item
103
fCheatsheet.addItem(newItem);
104         }
105     }
106
107     /**
108      * @return
109      */

110     private ISimpleCSItem createNewItem() {
111         ISimpleCSModelFactory factory = fCheatsheet.getModel().getFactory();
112         // Create the new item
113
// Element: item
114
ISimpleCSItem item = factory.createSimpleCSItem(fCheatsheet);
115         item.setTitle(generateItemTitle(PDEUIMessages.SimpleCheatSheetCreationOperation_1));
116         // Element: description
117
ISimpleCSDescription description = factory.createSimpleCSDescription(item);
118         description.setContent(
119                 BaseCSCreationOperation.formatTextBold(
120                         PDEUIMessages.SimpleCheatSheetCreationOperation_2));
121         item.setDescription(description);
122         return item;
123     }
124     
125     /**
126      * @return
127      */

128     private String JavaDoc generateItemTitle(String JavaDoc base) {
129         StringBuffer JavaDoc result = new StringBuffer JavaDoc(base);
130         ISimpleCSItem[] items = fCheatsheet.getItems();
131         // Used to track auto-generated numbers used
132
HashSet JavaDoc set = new HashSet JavaDoc();
133
134         // Linear search O(n).
135
// Performance hit unnoticeable because number of items per cheatsheet
136
// should be minimal.
137
for (int i = 0; i < items.length; i++) {
138             ISimpleCSItem item = items[i];
139             compareTitleWithBase(base, set, item.getTitle());
140         }
141         // Add an auto-generated number
142
addNumberToBase(result, set);
143         
144         return result.toString();
145     }
146     
147 }
148
Popular Tags