KickJava   Java API By Example, From Geeks To Geeks.

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


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.ISimpleCSConstants;
17 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem;
18 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory;
19 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject;
20 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem;
21 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject;
22 import org.eclipse.pde.internal.ui.PDEUIMessages;
23 import org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractAddAction;
24
25 /**
26  * SimpleCSAddStepAction
27  *
28  */

29 public class SimpleCSAddSubStepAction extends CSAbstractAddAction {
30
31     private ISimpleCSItem fItem;
32     
33     private ISimpleCSSubItem fSubitem;
34     
35     /**
36      *
37      */

38     public SimpleCSAddSubStepAction() {
39         setText(PDEUIMessages.SimpleCSAddSubStepAction_0);
40     }
41
42     /**
43      * @param cheatsheet
44      */

45     public void setDataObject(ISimpleCSObject csObject) {
46         // Determine input
47
if (csObject.getType() == ISimpleCSConstants.TYPE_ITEM) {
48             fSubitem = null;
49             fItem = (ISimpleCSItem)csObject;
50         } else if (csObject.getType() == ISimpleCSConstants.TYPE_SUBITEM) {
51             fSubitem = (ISimpleCSSubItem)csObject;
52             ISimpleCSObject parentObject = fSubitem.getParent();
53             // Determine input's parent object
54
if (parentObject.getType() == ISimpleCSConstants.TYPE_ITEM) {
55                 fItem = (ISimpleCSItem)parentObject;
56             } else if (parentObject.getType() == ISimpleCSConstants.TYPE_CONDITIONAL_SUBITEM) {
57                 // Not supported by editor, action will not run
58
fItem = null;
59             } else if (parentObject.getType() == ISimpleCSConstants.TYPE_REPEATED_SUBITEM) {
60                 // Note supported by editor, action will not run
61
fItem = null;
62             }
63         } else {
64             // Invalid input, action will not run
65
fSubitem = null;
66             fItem = null;
67         }
68     }
69
70     /* (non-Javadoc)
71      * @see org.eclipse.jface.action.Action#run()
72      */

73     public void run() {
74         // Ensure we have valid input
75
if (fItem == null) {
76             return;
77         }
78         // Create the new subitem
79
ISimpleCSSubItem newSubItem = createNewSubItem();
80         // Insert the new subitem
81
insertNewSubItem(newSubItem);
82     }
83
84     /**
85      * @return
86      */

87     private ISimpleCSSubItem createNewSubItem() {
88         ISimpleCSModelFactory factory = fItem.getModel().getFactory();
89         // Element: subitem
90
ISimpleCSSubItem subitem = factory.createSimpleCSSubItem(fItem);
91         // Set on the proper parent object
92
subitem.setLabel(generateSubItemLabel(fItem, PDEUIMessages.SimpleCSAddSubStepAction_1));
93         return subitem;
94     }
95     
96     /**
97      * @param newSubItem
98      */

99     private void insertNewSubItem(ISimpleCSSubItem newSubItem) {
100         // Insert the new subitem depending on the input specfied
101
if (fSubitem != null) {
102             // Subitem input object
103
// Insert subitem right after the input item object
104
int index = fItem.indexOfSubItem(fSubitem) + 1;
105             fItem.addSubItem(index, newSubItem);
106         } else {
107             // Item input object
108
// Insert subitem as the last child subitem
109
fItem.addSubItem(newSubItem);
110         }
111     }
112     
113     /**
114      * @return
115      */

116     private String JavaDoc generateSubItemLabel(ISimpleCSItem item, String JavaDoc base) {
117         StringBuffer JavaDoc result = new StringBuffer JavaDoc(base);
118         ISimpleCSSubItemObject[] subitems = item.getSubItems();
119         // Used to track auto-generated numbers used
120
HashSet JavaDoc set = new HashSet JavaDoc();
121
122         // Linear search O(n).
123
// Performance hit unnoticeable because number of items per cheatsheet
124
// should be minimal.
125
for (int i = 0; i < subitems.length; i++) {
126             ISimpleCSSubItemObject object = subitems[i];
127             if (object.getType() == ISimpleCSConstants.TYPE_SUBITEM) {
128                 ISimpleCSSubItem subitem = (ISimpleCSSubItem)object;
129                 compareTitleWithBase(base, set, subitem.getLabel());
130             }
131         }
132         // Add an auto-generated number
133
addNumberToBase(result, set);
134         
135         return result.toString();
136     }
137 }
138
Popular Tags