• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.editors.layout;
18 
19 import com.android.ide.eclipse.adt.internal.editors.descriptors.ElementDescriptor;
20 import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData;
21 
22 import org.eclipse.gef.palette.PaletteDrawer;
23 import org.eclipse.gef.palette.PaletteGroup;
24 import org.eclipse.gef.palette.PaletteRoot;
25 import org.eclipse.gef.palette.PaletteTemplateEntry;
26 
27 import java.util.List;
28 
29 /**
30  * Factory that creates the palette for the {@link GraphicalLayoutEditor}.
31  *
32  * @since GLE1
33  */
34 public class PaletteFactory {
35 
36     /** Static factory, nothing to instantiate here. */
PaletteFactory()37     private PaletteFactory() {
38     }
39 
createPaletteRoot(PaletteRoot currentPalette, AndroidTargetData targetData)40     public static PaletteRoot createPaletteRoot(PaletteRoot currentPalette,
41             AndroidTargetData targetData) {
42 
43         if (currentPalette == null) {
44             currentPalette = new PaletteRoot();
45         }
46 
47         for (int n = currentPalette.getChildren().size() - 1; n >= 0; n--) {
48             currentPalette.getChildren().remove(n);
49         }
50 
51         if (targetData != null) {
52             addTools(currentPalette);
53             addViews(currentPalette, "Layouts",
54                     targetData.getLayoutDescriptors().getLayoutDescriptors());
55             addViews(currentPalette, "Views",
56                     targetData.getLayoutDescriptors().getViewDescriptors());
57         }
58 
59         return currentPalette;
60     }
61 
addTools(PaletteRoot paletteRoot)62     private static void addTools(PaletteRoot paletteRoot) {
63         PaletteGroup group = new PaletteGroup("Tools");
64 
65         // Default tools: selection.
66         // Do not use the MarqueeToolEntry since we don't support multiple selection.
67         /* -- Do not put the selection tool. It's the unique tool so it looks useless.
68               Leave this piece of code here in case we want it back later.
69         PanningSelectionToolEntry entry = new PanningSelectionToolEntry();
70         group.add(entry);
71         paletteRoot.setDefaultEntry(entry);
72         */
73 
74         paletteRoot.add(group);
75     }
76 
addViews(PaletteRoot paletteRoot, String groupName, List<ElementDescriptor> descriptors)77     private static void addViews(PaletteRoot paletteRoot, String groupName,
78             List<ElementDescriptor> descriptors) {
79         PaletteDrawer group = new PaletteDrawer(groupName);
80 
81         for (ElementDescriptor desc : descriptors) {
82             PaletteTemplateEntry entry = new PaletteTemplateEntry(
83                     desc.getUiName(),           // label
84                     desc.getTooltip(),          // short description
85                     desc,                       // template
86                     desc.getImageDescriptor(),  // small icon
87                     desc.getImageDescriptor()   // large icon
88                     );
89 
90             group.add(entry);
91         }
92 
93         paletteRoot.add(group);
94     }
95 }
96