Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eaa10e597b0fbb36542d7af9757540dc364b2def (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/***********************************************************************
 * Copyright (c) 2009 Anyware Technologies 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:
 *    Jacques Lescot (Anyware Technologies) - initial API and implementation
 **********************************************************************/
package org.eclipse.papyrus.views.documentation.view;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.views.documentation.DocumentationUnsupportedException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

/**
 * A class defining an composite used to edit the documentation related to a semantic element.<br>
 * <br>
 * Created : 11 August 2009<br>
 * 
 * @author <a href="mailto:jacques.lescot@anyware-tech.com">Jacques LESCOT</a>
 */
public class CommentsComposite extends DocPageComposite
{

    /** A composite used to edit the documentation using a Rich Text editor */
    private RichTextComposite richTextComposite;

    /** A composite used to edit the documentation using a Plain Text editor */
    private PlainTextComposite plainTextComposite;

    private Button useRichTextEditorButton;

    private Button editButton;

    private Composite editorContainer;

    private FocusListener focusListener;

    private Text infoText;

    private static String typeLabel = Messages.CommentsComposite_docCurrent;

    private static boolean isRichTextEnabled = true;

    /**
     * Constructor.
     * 
     * @param parent the parent composite
     * @param style the composite style
     * @param editingDomainProvider the provider of editing domain to execute commands
     */
    public CommentsComposite(Composite parent, int style)
    {
        super(parent, style);
    }

    /**
     * @see org.topcased.modeler.documentation.DocPageComposite#createContents(org.eclipse.swt.widgets.Composite)
     */
    protected void createContents(Composite parent)
    {
        boolean labelForTypeInformation = (getStyle() & DocPage.STYLE_TEXT_TYPE) != 0;
        if (labelForTypeInformation)
        {
            infoText = new Text(parent, SWT.READ_ONLY);
        }

        parent.setLayout(new GridLayout(3, false));

        editorContainer = new Composite(parent, SWT.NONE);
        GridData gData = new GridData(GridData.FILL_BOTH);
        gData.horizontalSpan = 3;
        editorContainer.setLayoutData(gData);
        editorContainer.setLayout(new FillLayout());

        if (isRichTextEnabled)
        {
            try
            {
                Browser browser = new Browser(editorContainer, SWT.NONE);
                browser.dispose();
            }
            catch (SWTError swtError)
            {
            	// TODO log
//                ModelerPlugin.log("None XULRunner installed. You can not use Rich Text Edition.", IStatus.WARNING);
                isRichTextEnabled = false;
            }
        }

        // Initialize Simple Text Editor by default
        plainTextComposite = createPlainCommentsComposite();

        useRichTextEditorButton = new Button(parent, SWT.CHECK | SWT.RIGHT);
        useRichTextEditorButton.setText("Use HTML Editor");
        useRichTextEditorButton.setImage(DocViewPlugin.getDefault().getImageRegistry().get("HTML_EDITOR"));
        useRichTextEditorButton.setSelection(false);
        useRichTextEditorButton.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
                changeEditor();
            }
        });

        Label emptyLbl = new Label(parent, SWT.NONE);
        emptyLbl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        editButton = new Button(parent, SWT.PUSH);
        editButton.setImage(DocViewPlugin.getDefault().getImageRegistry().get("EDIT"));
        editButton.setText("Edit");
        editButton.setEnabled(useRichTextEditorButton.getSelection());
        editButton.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
                RichTextEditorDialog dialog = new RichTextEditorDialog(getShell(), getDocumentationValueFromElement());
                if (dialog.open() == Window.OK)
                {
                    richTextComposite.setDocumentationValue(dialog.getDocumentationValue());
                    handleDocChanged();
                }

            }
        });

        if (infoText != null)
        {
            infoText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        }
    }

    /**
     * @see org.topcased.modeler.documentation.DocPageComposite#refresh()
     */
    protected void refresh()
    {
    	EObject docElement = getDocumentedElement();
        useRichTextEditorButton.setEnabled(docElement != null);
        if (useRichTextEditorButton.getSelection())
        {
            richTextComposite.setDocumentationValue("");
            if (docElement != null)
            {
                richTextComposite.setDocumentationValue(getDocumentationValueFromElement());
            }
        }
        else
        {
            plainTextComposite.setDocumentationValue("");
            plainTextComposite.getControl().setEnabled(docElement != null);
            if (docElement != null)
            {
                plainTextComposite.setDocumentationValue(getDocumentationValueFromElement());
            }
        }
        if (infoText != null) {
        	if (docElement != null) {
        		infoText.setText(typeLabel + docElement.eClass().getName());
        	} else {
        		infoText.setText("");
        	}
        }
    }

    /**
     * @return the richTextComposite
     */
    protected RichTextComposite getRichTextComposite()
    {
        return richTextComposite;
    }

    /**
     * @return the plainTextComposite
     */
    public PlainTextComposite getPlainTextComposite()
    {
        return plainTextComposite;
    }

    /**
     * Change comments editor between simple Text Editor to HTML Text Editor and
     */
    public void changeEditor()
    {
        if (!useRichTextEditorButton.getSelection())
        {
            String comment = richTextComposite.getDocumentationValue();
            richTextComposite.dispose();
            plainTextComposite = createPlainCommentsComposite();
            plainTextComposite.setFocus();
            plainTextComposite.setDocumentationValue(comment);
            useRichTextEditorButton.setToolTipText("Use HTML Editor to edit documentation");
            useRichTextEditorButton.setSelection(false);
            editButton.setEnabled(false);
        }
        else
        {
            if (isRichTextEnabled)
            {
                String comment = plainTextComposite.getDocumentationValue();
                plainTextComposite.getControl().removeFocusListener(focusListener);
                plainTextComposite.dispose();
                richTextComposite = createRichCommentsComposite();
                richTextComposite.setDocumentationValue(comment);
                useRichTextEditorButton.setToolTipText("Use Text Editor to edit documentation");
                useRichTextEditorButton.setSelection(true);
                editButton.setEnabled(true);
                editButton.setFocus();
            }
            else
            {
                showMessageDialog();
                useRichTextEditorButton.setSelection(false);
            }
        }
        editorContainer.layout();
    }

    private void showMessageDialog()
    {
        MessageDialog dialog = new MessageDialog(getShell(), "Rich Text Edition unavailable", null,
                "Your operating system does not support the SWT Browser component, thus you are not able to edit documentation using rich text capabilities.\n"
                        + "You should try installing XULRunner in your environment so that you can enable Rich Text Edition, and then restart your application.\n"
                        + "To install XulRunner, have a look at : <a>http://www.eclipse.org/atf/downloads/base_files/manualXulrunner_section.php</a>.", MessageDialog.WARNING,
                new String[] {IDialogConstants.OK_LABEL}, 0)
        {
            @Override
            protected Control createMessageArea(Composite composite)
            {
                Image image = getImage();
                if (image != null)
                {
                    imageLabel = new Label(composite, SWT.NULL);
                    image.setBackground(imageLabel.getBackground());
                    imageLabel.setImage(image);
                    GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.BEGINNING).applyTo(imageLabel);
                }
                // create message
                if (message != null)
                {
                    Link messageLink = new Link(composite, getMessageLabelStyle());
                    messageLink.setText(message);
                    messageLink.addListener(SWT.Selection, new Listener()
                    {
                        public void handleEvent(Event event)
                        {
                            try
                            {
                                PlatformUI.getWorkbench().getBrowserSupport().createBrowser(null).openURL(new URL(event.text));
                                okPressed();
                            }
                            catch (PartInitException e)
                            {
                            	// TODO log
                            }
                            catch (MalformedURLException e)
                            {
                            	// TODO log
                            }
                        }
                    });
                    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).hint(convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH), SWT.DEFAULT).applyTo(
                            messageLink);
                }
                return composite;
            }
        };
        dialog.open();
    }

    /**
     * Create the Composite used to render the Rich Text Editor
     * 
     * @return RichTextCommentsComposite
     */
    protected RichTextComposite createRichCommentsComposite()
    {
        richTextComposite = new RichTextComposite(editorContainer, SWT.NONE);

        GridLayout layout = new GridLayout();
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        richTextComposite.setLayout(layout);

        return richTextComposite;
    }

    /**
     * Create the Composite used to render the Plain text editor
     * 
     * @return PlainTextComposite
     */
    protected PlainTextComposite createPlainCommentsComposite()
    {
        plainTextComposite = new PlainTextComposite(editorContainer, SWT.NONE);
        focusListener = new FocusAdapter()
        {
            private String previousComment;

            public void focusGained(FocusEvent e)
            {
                previousComment = plainTextComposite.getDocumentationValue();
            }

            public void focusLost(FocusEvent e)
            {
                if (!plainTextComposite.getDocumentationValue().equals(previousComment))
                {
                    handleDocChanged();
                }
            }
        };

        plainTextComposite.getTextControl().addFocusListener(focusListener);
        return plainTextComposite;
    }

    /**
     * @see org.eclipse.swt.widgets.Composite#setFocus()
     */
    public boolean setFocus()
    {
        if (useRichTextEditorButton == null)
        {
            return false;
        }
        if (useRichTextEditorButton.getSelection())
        {
            return editButton.setFocus();
        }
        if (plainTextComposite == null)
        {
            return false;
        }
        return plainTextComposite.setFocus();
    }

    /**
     * Gets the checkbox allowing to use rich text edition
     * 
     * @return the useRichTextEditorButton
     */
    protected Button getUseRichTextEditorButton()
    {
        return useRichTextEditorButton;
    }

    /**
     * Gets the edit button
     * 
     * @return the edit button
     */
    protected Button getEditButton()
    {
        return editButton;
    }

    /**
     * Get the String value of the element to be edited.<br/>
     * 
     * @return String the documentation text
     */
    protected String getDocumentationValueFromElement()
    {
    	try {
    		IDocumentationPartHandler documentationPartHandler = DocumentionPartHandlerRegistry.getInstance().getDocumentationPartHandler(getActivePart());
    		if (documentationPartHandler != null) {
    			EObject associatedDiagram = documentationPartHandler.getAssociatedDiagram(getActivePart(), getDocumentedElement());
    			EObject toDocument = associatedDiagram == null ? getDocumentedElement() : associatedDiagram;
    			return documentationPartHandler.getDocumentationManager().getDocumentation(toDocument);
    		}
		} catch (DocumentationUnsupportedException e) {
		}
		return ""; //$NON-NLS-1$
    }

    /**
     * Update documentation text using a Command.
     */
    public void handleDocChanged()
    {
    	IDocumentationPartHandler documentationPartHandler = DocumentionPartHandlerRegistry.getInstance().getDocumentationPartHandler(getActivePart());
    	if (documentationPartHandler != null) {
    		String docValue = getUseRichTextEditorButton().getSelection() ? getRichTextComposite().getDocumentationValue() : getPlainTextComposite().getDocumentationValue();
    		Command cmd = documentationPartHandler.getDocumentationManager().getChangeDocumentationCommand(getDocumentedElement(), docValue);
    		documentationPartHandler.executeCommand(getActivePart(), cmd);
    	}
    }

    @Override
    public void setEnabled(boolean enabled)
    {
        if (plainTextComposite != null)
        {
            plainTextComposite.setEnabled(enabled);
        }
        if (richTextComposite != null)
        {
            richTextComposite.setEnabled(enabled);
        }
        if (useRichTextEditorButton != null)
        {
            useRichTextEditorButton.setEnabled(enabled);
        }
    }

    @Override
    public void setForeground(Color color)
    {
        super.setForeground(color);
        if (plainTextComposite != null)
        {
            plainTextComposite.setForeground(color);
        }
        if (richTextComposite != null)
        {
            richTextComposite.setForeground(color);
        }
    }

}

Back to the top