Skip to main content
summaryrefslogtreecommitdiffstats
blob: 117eafc80a90645ef440f58aa4ecd1c8512188f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*******************************************************************************
 * Copyright (c) 2001, 2008 Oracle Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Oracle Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.ui.internal.component;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.IInputProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.jst.jsf.common.dom.TagIdentifier;
import org.eclipse.jst.jsf.common.runtime.internal.model.ViewObject;
import org.eclipse.jst.jsf.common.runtime.internal.model.component.ComponentInfo;
import org.eclipse.jst.jsf.common.runtime.internal.model.decorator.ConverterDecorator;
import org.eclipse.jst.jsf.common.runtime.internal.model.decorator.Decorator;
import org.eclipse.jst.jsf.common.runtime.internal.model.decorator.ValidatorDecorator;
import org.eclipse.jst.jsf.common.ui.JSFUICommonPlugin;
import org.eclipse.jst.jsf.common.ui.internal.form.AbstractMasterForm;
import org.eclipse.jst.jsf.common.ui.internal.utils.JSFSharedImages;
import org.eclipse.jst.jsf.designtime.internal.view.DTUIViewRoot;
import org.eclipse.jst.jsf.designtime.internal.view.XMLViewObjectMappingService;
import org.eclipse.jst.jsf.designtime.internal.view.XMLViewObjectMappingService.ElementData;
import org.eclipse.jst.jsf.ui.internal.common.MetadataTagImageManager;
import org.eclipse.jst.jsf.ui.internal.component.ComponentTreeViewProvider.TreePlaceHolder;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.FormToolkit;

/*package*/class ComponentMasterForm extends AbstractMasterForm
{
    private final DTJSFViewModel _model;
    private TreeViewer           _treeViewer;
    private Action               _refreshAction;

    protected ComponentMasterForm(final FormToolkit toolkit,
            final DTJSFViewModel model)
    {
        super(toolkit);
        _model = model;
    }

    @Override
    public Control createClientArea(final Composite parent)
    {
        final Tree tree = getToolkit().createTree(parent,
                SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
        final GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
        tree.setLayoutData(gridData);

        _treeViewer = new TreeViewer(tree);
        _treeViewer.getTree().setLayoutData(gridData);
        _treeViewer.setContentProvider(new ComponentTreeViewProvider());
        _treeViewer.setLabelProvider(new TreeViewLabelProvider(_treeViewer));
        _treeViewer.setSorter(new ComponentTreeSorter());
        _treeViewer.addSelectionChangedListener(new ISelectionChangedListener()
        {
            public void selectionChanged(final SelectionChangedEvent event)
            {
                getListener().selectionChanged(event);
            }

        });

        _model.init(new Runnable()
        {
            public void run()
            {
                PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable()
                {
                    public void run()
                    {
                        final Object[] expanded = _treeViewer
                                .getExpandedElements();
                        _treeViewer.refresh();
                        _treeViewer.setExpandedElements(expanded);
                    }
                });
            }
        });

        _treeViewer.setInput(_model);

        makeActions();
        return tree;
    }

    @Override
    protected void contributeActions(final IToolBarManager formManager,
            final IToolBarManager localManager)
    {
        // contribute to local tool bar
        localManager.add(_refreshAction);
        localManager.update(false);
    }

    private void makeActions()
    {
        _refreshAction = new Action()
        {
            @Override
            public void run()
            {
                _model.update();
            }
        };
        _refreshAction.setText(Messages.ComponentMasterForm_RefreshView);
        _refreshAction.setToolTipText(Messages.ComponentMasterForm_RefreshView);
        _refreshAction.setImageDescriptor(JSFUICommonPlugin.getDefault()
                .getImageDescriptor("refresh_nav_16.gif")); //$NON-NLS-1$
    }

    @Override
    protected void contributeToHeadArea(final FormToolkit toolkit,
            final Composite container)
    {
        final Text label = new Text(container
                , SWT.READ_ONLY | SWT.SINGLE | SWT.LEFT);

        String viewId = _model.getViewId();
        viewId = viewId == null ? Messages.ComponentMasterForm_Unknown : viewId;        
        label.setText(NLS.bind(Messages.ComponentMasterForm_HeadAreaText,  viewId,
                _model.getProject().getName())); 
    }

    private static class TreeViewLabelProvider extends LabelProvider
    {
        private final IInputProvider          _inputProvider;
        private final MetadataTagImageManager _metadataIconManager;

        /**
         * @param inputProvider
         */
        private TreeViewLabelProvider(final IInputProvider inputProvider)
        {
            super();
            _inputProvider = inputProvider;
            _metadataIconManager = new MetadataTagImageManager();
        }

        @Override
        public String getText(final Object obj)
        {
            if (obj instanceof ComponentInfo)
            {
                String text = ""; //$NON-NLS-1$
                final ComponentInfo compInfo = (ComponentInfo) obj;
                String className = compInfo.getComponentTypeInfo()
                        .getClassName();
                final int dotIdx = className.lastIndexOf('.');
                if (dotIdx > -1 && dotIdx + 1 < className.length())
                {
                    className = className.substring(dotIdx + 1);
                }
                text = className;

                final String id = compInfo.getId();

                if (id != null)
                {
                    text += " (id=" + id + ")"; //$NON-NLS-1$ //$NON-NLS-2$
                }
                return text;
            }
            else if (obj instanceof TreePlaceHolder)
            {
                return Messages.ComponentMasterForm_CalculatingView;
            }
            return obj.toString();
        }

        @Override
        public Image getImage(final Object obj)
        {
            Image image = null;
            if (obj instanceof ViewObject)
            {
                final XMLViewObjectMappingService mappingService = getMappingService();

                if (mappingService != null)
                {
                    final ElementData elementData = mappingService
                            .findElementData((ViewObject) obj);

                    if (elementData != null)
                    {
                        final TagIdentifier tagId = elementData.getTagId();
                        final DTJSFViewModel model = getModel();
                        if (model != null)
                        {
                            image = _metadataIconManager.getSmallIconImage(
                                    model.getFile(), tagId);
                        }
                    }
                }
                if (image == null)
                {
                    image = getDefaultImage((ViewObject) obj);
                }
            }
            else if (obj instanceof TreePlaceHolder)
            {
                return JSFUICommonPlugin.getDefault().getImage("configs.gif"); //$NON-NLS-1$
            }
            if (image == null)
            {
                final String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
                image = PlatformUI.getWorkbench().getSharedImages().getImage(
                        imageKey);
            }

            return image;
        }

        private XMLViewObjectMappingService getMappingService()
        {
            final DTJSFViewModel model = getModel();

            if (model != null)
            {
                final DTUIViewRoot root = model.getRoot();

                if (root != null)
                {
                    return (XMLViewObjectMappingService) root.getServices()
                            .getAdapter(XMLViewObjectMappingService.class);
                }
            }
            return null;
        }

        private DTJSFViewModel getModel()
        {
            final Object input = _inputProvider.getInput();

            if (input instanceof DTJSFViewModel)
            {
                return (DTJSFViewModel) input;
            }
            return null;
        }

        private Image getDefaultImage(final ViewObject obj)
        {
            if (obj instanceof DTUIViewRoot)
            {
                return JSFUICommonPlugin.getDefault().getImage(
                        JSFSharedImages.GENERIC_VIEWROOT_IMG);
            }
            if (obj instanceof ComponentInfo)
            {
                return JSFUICommonPlugin.getDefault().getImage(
                        JSFSharedImages.GENERIC_OBJECT_IMG);
            }
            else if (obj instanceof ConverterDecorator)
            {
                return JSFUICommonPlugin.getDefault().getImage(
                        JSFSharedImages.GENERIC_CONVERTER_IMG);
            }
            else if (obj instanceof ValidatorDecorator)
            {
                return JSFUICommonPlugin.getDefault().getImage(
                        JSFSharedImages.GENERIC_VALIDATOR_IMG);
            }
            return JSFUICommonPlugin.getDefault().getImage(
                    JSFSharedImages.DEFAULT_PALETTE_TAG_IMG);
        }
    }

    private static class ComponentTreeSorter extends ViewerSorter
    {
        @Override
        public int compare(final Viewer viewer, final Object e1, final Object e2)
        {
            return 0;
        }

        @Override
        public int category(final Object element)
        {
            // sort decorators first into their own category
            if (element instanceof Decorator)
            {
                return 0;
            }
            return 1;
        }
    }
}

Back to the top