Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a892a83

Browse files
committed
StyledTextControl doesn't set SWT.BORDER by default and add option to not render links
- StyledTextControl can be used without a border and without rendering links - Refactor some things for re-use
1 parent 6e43f79 commit a892a83

8 files changed

Lines changed: 71 additions & 32 deletions

File tree

com.archimatetool.canvas/src/com/archimatetool/canvas/propertysections/HintContentSection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected void textChanged(String oldText, String newText) {
9494

9595
createLabel(parent, Messages.HintContentSection_3, ITabbedLayoutConstants.STANDARD_LABEL_WIDTH, SWT.NONE);
9696

97-
StyledTextControl styledTextControl = createStyledTextControl(parent, SWT.NONE);
97+
StyledTextControl styledTextControl = createStyledTextControl(parent, SWT.BORDER);
9898
styledTextControl.setMessage(Messages.HintContentSection_5);
9999

100100
fTextContentControl = new PropertySectionTextControl(styledTextControl.getControl(), ICanvasPackage.Literals.HINT_PROVIDER__HINT_CONTENT) {

com.archimatetool.canvas/src/com/archimatetool/canvas/propertysections/NotesSection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Class<?> getAdaptableType() {
5757
protected void createControls(Composite parent) {
5858
createLabel(parent, Messages.NotesSection_0, ITabbedLayoutConstants.STANDARD_LABEL_WIDTH, SWT.NONE);
5959

60-
StyledTextControl styledTextControl = createStyledTextControl(parent, SWT.NONE);
60+
StyledTextControl styledTextControl = createStyledTextControl(parent, SWT.BORDER);
6161
styledTextControl.setMessage(Messages.NotesSection_2);
6262

6363
fTextNotesControl = new PropertySectionTextControl(styledTextControl.getControl(), ICanvasPackage.Literals.NOTES_CONTENT__NOTES) {

com.archimatetool.editor/src/com/archimatetool/editor/propertysections/AbstractArchiPropertySection.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.HashMap;
99
import java.util.Map;
1010

11+
import org.eclipse.jface.layout.GridDataFactory;
1112
import org.eclipse.jface.layout.TableColumnLayout;
1213
import org.eclipse.jface.viewers.ISelection;
1314
import org.eclipse.jface.viewers.IStructuredSelection;
@@ -146,17 +147,24 @@ protected Text createSingleTextControl(Composite parent, int style) {
146147
* @return A StyledTextControl
147148
*/
148149
protected StyledTextControl createStyledTextControl(Composite parent, int style) {
149-
StyledTextControl styledTextControl = new StyledTextControl(parent, style | SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
150+
return createStyledTextControl(parent, style, true);
151+
}
152+
153+
/**
154+
* @param parent Parent
155+
* @param style The style
156+
* @param renderLinks if true will render links
157+
* @return A StyledTextControl
158+
*/
159+
protected StyledTextControl createStyledTextControl(Composite parent, int style, boolean renderLinks) {
160+
StyledTextControl styledTextControl = new StyledTextControl(parent, style | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP, renderLinks);
150161

151162
// Add listener to disable global actions when it gets the focus
152163
addGlobalActionDisablementListener(styledTextControl.getControl());
153164

154-
//Text textControl = getWidgetFactory().createText(parent, null, SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
155-
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
156-
// This stops excess size if the control contains a lot of text
157-
gd.widthHint = 100;
158-
gd.heightHint = 100;
159-
styledTextControl.getControl().setLayoutData(gd);
165+
// 100 width and height hint stops excess size if the control contains a lot of text
166+
GridDataFactory.create(GridData.FILL_BOTH).hint(100, 100).applyTo(styledTextControl.getControl());
167+
160168
return styledTextControl;
161169
}
162170

com.archimatetool.editor/src/com/archimatetool/editor/propertysections/AbstractECorePropertySection.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.eclipse.gef.commands.CompoundCommand;
1717
import org.eclipse.jface.viewers.IStructuredSelection;
1818
import org.eclipse.swt.SWT;
19+
import org.eclipse.swt.custom.StyledText;
1920
import org.eclipse.swt.widgets.Composite;
2021
import org.eclipse.swt.widgets.Text;
2122

@@ -281,10 +282,17 @@ protected PropertySectionTextControl createDocumentationControl(Composite parent
281282
createLabel(parent, Messages.AbstractECorePropertySection_2, ITabbedLayoutConstants.STANDARD_LABEL_WIDTH, SWT.NONE);
282283

283284
// Text
284-
StyledTextControl styledTextControl = createStyledTextControl(parent, SWT.NONE);
285+
StyledTextControl styledTextControl = createStyledTextControl(parent, SWT.BORDER);
285286
styledTextControl.setMessage(hint);
286287

287-
PropertySectionTextControl textDoc = new PropertySectionTextControl(styledTextControl.getControl(), IArchimatePackage.Literals.DOCUMENTABLE__DOCUMENTATION) {
288+
return createDocumentationPropertySectionTextControl(styledTextControl.getControl());
289+
}
290+
291+
/**
292+
* Create a PropertySectionTextControl for Documentation
293+
*/
294+
protected PropertySectionTextControl createDocumentationPropertySectionTextControl(StyledText styledText) {
295+
return new PropertySectionTextControl(styledText, IArchimatePackage.Literals.DOCUMENTABLE__DOCUMENTATION) {
288296
@Override
289297
protected void textChanged(String oldText, String newText) {
290298
if(getEObjects() != null) {
@@ -304,7 +312,5 @@ protected void textChanged(String oldText, String newText) {
304312
}
305313
}
306314
};
307-
308-
return textDoc;
309315
}
310316
}

com.archimatetool.editor/src/com/archimatetool/editor/propertysections/ArchimateModelSection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private void createPurposeControl(Composite parent) {
7373
createLabel(parent, Messages.ArchimateModelSection_2, STANDARD_LABEL_WIDTH, SWT.NONE);
7474

7575
// Text
76-
StyledTextControl styledTextControl = createStyledTextControl(parent, SWT.NONE);
76+
StyledTextControl styledTextControl = createStyledTextControl(parent, SWT.BORDER);
7777
styledTextControl.setMessage(Messages.ArchimateModelSection_4);
7878

7979
fTextPurpose = new PropertySectionTextControl(styledTextControl.getControl(), IArchimatePackage.Literals.ARCHIMATE_MODEL__PURPOSE) {

com.archimatetool.editor/src/com/archimatetool/editor/propertysections/LabelRendererSection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected void notifyChanged(Notification msg) {
6262
protected void createControls(Composite parent) {
6363
createLabel(parent, Messages.LabelRendererSection_0, ITabbedLayoutConstants.STANDARD_LABEL_WIDTH, SWT.NONE);
6464

65-
StyledTextControl styledTextControl = createStyledTextControl(parent, SWT.NONE);
65+
StyledTextControl styledTextControl = createStyledTextControl(parent, SWT.BORDER);
6666
styledTextControl.setMessage(Messages.LabelRendererSection_1);
6767

6868
fTextRender = new PropertySectionTextControl(styledTextControl.getControl(), TextRenderer.FEATURE_NAME) {

com.archimatetool.editor/src/com/archimatetool/editor/propertysections/TextContentSection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Class<?> getAdaptableType() {
5252
protected void createControls(Composite parent) {
5353
createLabel(parent, Messages.TextContentSection_0, ITabbedLayoutConstants.STANDARD_LABEL_WIDTH, SWT.NONE);
5454

55-
StyledTextControl styledTextControl = createStyledTextControl(parent, SWT.NONE);
55+
StyledTextControl styledTextControl = createStyledTextControl(parent, SWT.BORDER);
5656
styledTextControl.setMessage(Messages.TextContentSection_2);
5757

5858
fTextContentControl = new PropertySectionTextControl(styledTextControl.getControl(), IArchimatePackage.Literals.TEXT_CONTENT__CONTENT) {

com.archimatetool.editor/src/com/archimatetool/editor/ui/components/StyledTextControl.java

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import org.eclipse.jface.action.Action;
1414
import org.eclipse.jface.action.IAction;
15-
import org.eclipse.jface.action.IMenuListener;
1615
import org.eclipse.jface.action.IMenuManager;
1716
import org.eclipse.jface.action.MenuManager;
1817
import org.eclipse.jface.action.Separator;
@@ -77,6 +76,9 @@ private class LinkInfo {
7776
private String originalText = "", editedText = ""; //$NON-NLS-1$ //$NON-NLS-2$
7877
private String message;
7978

79+
// Whether to render http ftp type text as links
80+
private boolean renderLinks = true;
81+
8082
private Listener eventListener = this::handleEvent;
8183
private LineStyleListener lineStyleListener = this::lineGetStyle;
8284
private VerifyKeyListener verifyKeyListener = this::handleVerifyKey;
@@ -125,22 +127,34 @@ public void run() {
125127
};
126128

127129
public StyledTextControl(Composite parent, int style) {
128-
this(new StyledText(parent, style));
130+
this(parent, style, true);
129131
}
130132

131133
public StyledTextControl(StyledText styledText) {
134+
this(styledText, true);
135+
}
136+
137+
public StyledTextControl(Composite parent, int style, boolean renderLinks) {
138+
this(new StyledText(parent, style), renderLinks);
139+
}
140+
141+
public StyledTextControl(StyledText styledText, boolean renderLinks) {
132142
fStyledText = styledText;
133143
fStyledText.setLeftMargin(PlatformUtils.isWindows() ? 4 : 2);
134144
fStyledText.setKeyBinding(ST.SELECT_ALL, ST.SELECT_ALL);
135145

136-
fHandCursor = new Cursor(styledText.getDisplay(), SWT.CURSOR_HAND);
146+
this.renderLinks = renderLinks;
137147

138148
for(int type : eventTypes) {
139149
fStyledText.addListener(type, eventListener);
140150
}
141151

142-
// Line Style
143-
fStyledText.addLineStyleListener(lineStyleListener);
152+
if(renderLinks) {
153+
// Cursor for links
154+
fHandCursor = new Cursor(styledText.getDisplay(), SWT.CURSOR_HAND);
155+
// Line Style
156+
fStyledText.addLineStyleListener(lineStyleListener);
157+
}
144158

145159
// Key presses
146160
fStyledText.addVerifyKeyListener(verifyKeyListener);
@@ -211,14 +225,11 @@ private void handleVerifyKey(VerifyEvent event) {
211225
* Hook into a right-click menu
212226
*/
213227
private void hookContextMenu() {
214-
MenuManager menuMgr = new MenuManager("#PopupMenu1"); //$NON-NLS-1$
228+
MenuManager menuMgr = new MenuManager("#StyledTextPopup"); //$NON-NLS-1$
215229
menuMgr.setRemoveAllWhenShown(true);
216230

217-
menuMgr.addMenuListener(new IMenuListener() {
218-
@Override
219-
public void menuAboutToShow(IMenuManager manager) {
220-
fillContextMenu(manager);
221-
}
231+
menuMgr.addMenuListener(manager -> {
232+
fillContextMenu(manager);
222233
});
223234

224235
Menu menu = menuMgr.createContextMenu(fStyledText);
@@ -267,6 +278,10 @@ public StyledText getControl() {
267278
* Scan for links in the text
268279
*/
269280
private void scanLinks() {
281+
if(!renderLinks) {
282+
return;
283+
}
284+
270285
fLinkInfos = new ArrayList<>();
271286
Matcher matcher = HTMLUtils.HTML_LINK_PATTERN.matcher(fStyledText.getText());
272287

@@ -281,9 +296,11 @@ private void scanLinks() {
281296
* otherwise returns null
282297
*/
283298
private String getLinkAt(int offset) {
284-
for(LinkInfo info : fLinkInfos) {
285-
if(offset >= info.start && offset < info.end) {
286-
return info.link;
299+
if(fLinkInfos != null) {
300+
for(LinkInfo info : fLinkInfos) {
301+
if(offset >= info.start && offset < info.end) {
302+
return info.link;
303+
}
287304
}
288305
}
289306
return null;
@@ -327,6 +344,10 @@ private void handleEvent(Event event) {
327344
* Mouse Up - Open link if Mod Key is pressed and on link
328345
*/
329346
private void doMouseUp(Event e) {
347+
if(!renderLinks) {
348+
return;
349+
}
350+
330351
if(isModKeyPressed(e)) {
331352
// Open link in Browser
332353
int offset = fStyledText.getOffsetAtPoint(new Point(e.x, e.y));
@@ -346,6 +367,10 @@ private void doMouseUp(Event e) {
346367
* Mouse Move - Update cursor if Mod Key is pressed
347368
*/
348369
private void doMouseMove(Event e) {
370+
if(!renderLinks) {
371+
return;
372+
}
373+
349374
if(isModKeyPressed(e)) {
350375
int offset = fStyledText.getOffsetAtPoint(new Point(e.x, e.y));
351376
if(getLinkAt(offset) != null) {
@@ -365,7 +390,7 @@ private void doMouseMove(Event e) {
365390
*/
366391
private void doKeyDown(Event e) {
367392
// Hand Cursor
368-
if(e.keyCode == SWT.MOD1) {
393+
if(renderLinks && e.keyCode == SWT.MOD1) {
369394
Point pt = fStyledText.getDisplay().getCursorLocation();
370395
pt = fStyledText.toControl(pt);
371396
int offset = fStyledText.getOffsetAtPoint(pt);
@@ -408,7 +433,7 @@ private void checkUndoPressed(Event e) {
408433
* Key up
409434
*/
410435
private void doKeyUp(Event e) {
411-
if(e.keyCode == SWT.MOD1) {
436+
if(renderLinks && e.keyCode == SWT.MOD1) {
412437
setCursor(null);
413438
}
414439
}

0 commit comments

Comments
 (0)