-
Notifications
You must be signed in to change notification settings - Fork 77
Update #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update #59
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| bin.includes = META-INF/,\ | ||
| .,\ | ||
| fragment.e4xmi | ||
| fragment.e4xmi,\ | ||
| plugin.xml | ||
| source.. = src/ | ||
| output.. = bin/ |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,17 @@ | ||||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||||
| <?eclipse version="3.4"?> | ||||||
| <plugin> | ||||||
| <extension | ||||||
| id="id1" | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The extension ID
Suggested change
|
||||||
| point="org.eclipse.e4.workbench.model"> | ||||||
| <processor | ||||||
| apply="always" | ||||||
| beforefragment="true" | ||||||
| class="com.vogella.tasks.ui.contribute.processors.MenuProcessor"> | ||||||
| <element | ||||||
| id="org.eclipse.ui.file.menu"> | ||||||
| </element> | ||||||
| </processor> | ||||||
| </extension> | ||||||
|
|
||||||
| </plugin> | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package com.vogella.tasks.ui.contribute.dialogs; | ||
|
|
||
| import jakarta.inject.Inject; | ||
| import jakarta.inject.Named; | ||
|
|
||
| import org.eclipse.e4.ui.services.IServiceConstants; | ||
| import org.eclipse.jface.dialogs.Dialog; | ||
| import org.eclipse.swt.SWT; | ||
| import org.eclipse.swt.widgets.Composite; | ||
| import org.eclipse.swt.widgets.Control; | ||
| import org.eclipse.swt.widgets.Label; | ||
| import org.eclipse.swt.widgets.Shell; | ||
|
|
||
| public class ExitDialog extends Dialog { | ||
| @Inject | ||
| public ExitDialog(@Named(IServiceConstants. | ||
| ACTIVE_SHELL) Shell shell) { | ||
| super(shell); | ||
| } | ||
|
Comment on lines
+15
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's good practice to provide a title for a dialog window to give users context. You can override the @Inject
public ExitDialog(@Named(IServiceConstants.
ACTIVE_SHELL) Shell shell) {
super(shell);
}
@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText("Confirm Exit"); // Should be externalized
} |
||
|
|
||
| @Override | ||
| protected Control createDialogArea(Composite parent) { | ||
| Label label = new Label(parent, SWT.NONE); | ||
| label.setText("Closing this application may result in data loss. " | ||
| + "Are you sure you want that?"); | ||
|
Comment on lines
+24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return parent; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.vogella.tasks.ui.contribute.handlers; | ||
|
|
||
| import org.eclipse.e4.core.contexts.ContextInjectionFactory; | ||
| import org.eclipse.e4.core.contexts.IEclipseContext; | ||
| import org.eclipse.e4.core.di.annotations.Execute; | ||
| import org.eclipse.e4.ui.workbench.IWorkbench; | ||
| import org.eclipse.jface.window.Window; | ||
|
|
||
| import com.vogella.tasks.ui.contribute.dialogs.ExitDialog; | ||
|
|
||
| public class ExitHandlerWithCheck { | ||
| @Execute | ||
| public void execute(IEclipseContext context, IWorkbench workbench) { | ||
| ExitDialog dialog = ContextInjectionFactory. | ||
| make(ExitDialog.class, context); | ||
| if (dialog.open() == Window.OK) { | ||
| workbench.close(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,49 @@ | ||||||
| package com.vogella.tasks.ui.contribute.processors; | ||||||
|
|
||||||
| import java.util.ArrayList; | ||||||
| import java.util.List; | ||||||
|
|
||||||
| import jakarta.inject.Inject; | ||||||
| import jakarta.inject.Named; | ||||||
|
|
||||||
| import org.eclipse.e4.core.di.annotations.Execute; | ||||||
| import org.eclipse.e4.ui.model.application.ui.menu.MDirectMenuItem; | ||||||
| import org.eclipse.e4.ui.model.application.ui.menu.MMenu; | ||||||
| import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement; | ||||||
| import org.eclipse.e4.ui.workbench.modeling.EModelService; | ||||||
|
|
||||||
| import com.vogella.tasks.ui.contribute.handlers.ExitHandlerWithCheck; | ||||||
|
|
||||||
| public class MenuProcessor { | ||||||
|
|
||||||
| // the menu is injected based on the parameter | ||||||
| // defined in the extension point | ||||||
| @Inject | ||||||
| @Named("org.eclipse.ui.file.menu") | ||||||
| private MMenu menu; | ||||||
|
|
||||||
| @Execute | ||||||
| public void execute(EModelService modelService) { | ||||||
| // remove the old exit menu entry | ||||||
| if (!menu.getChildren().isEmpty()) { | ||||||
| List<MMenuElement> list = new ArrayList<>(); | ||||||
| for (MMenuElement element : menu.getChildren()) { | ||||||
| // use ID instead of label as label is later translated | ||||||
| if (element.getElementId() != null) { | ||||||
| if (element.getElementId().contains("exit")) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current approach of checking
Suggested change
|
||||||
| list.add(element); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| menu.getChildren().removeAll(list); | ||||||
| } | ||||||
|
Comment on lines
+28
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
|
|
||||||
| // now add a new menu entry | ||||||
| MDirectMenuItem menuItem = modelService.createModelElement(MDirectMenuItem.class); | ||||||
| menuItem.setLabel("Another Exit"); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| menuItem.setContributionURI("bundleclass://" | ||||||
| + "com.vogella.tasks.ui.contribute/" | ||||||
| + ExitHandlerWithCheck.class.getName()); | ||||||
|
Comment on lines
+44
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoding the bundle symbolic name |
||||||
| menu.getChildren().add(menuItem); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,58 +1,68 @@ | ||||||
| package com.vogella.tasks.ui.parts; | ||||||
|
|
||||||
| import org.eclipse.e4.core.di.annotations.Optional; | ||||||
| import org.eclipse.e4.core.di.extensions.EventTopic; | ||||||
| import org.eclipse.e4.ui.di.Persist; | ||||||
| import org.eclipse.e4.ui.model.application.ui.basic.MPart; | ||||||
| import org.eclipse.jface.resource.JFaceResources; | ||||||
| import org.eclipse.jface.resource.LocalResourceManager; | ||||||
| import org.eclipse.jface.resource.ResourceManager; | ||||||
| import static org.eclipse.jface.layout.GridDataFactory.fillDefaults; | ||||||
| import static org.eclipse.jface.widgets.WidgetFactory.button; | ||||||
| import static org.eclipse.jface.widgets.WidgetFactory.text; | ||||||
|
|
||||||
| import java.io.UnsupportedEncodingException; | ||||||
| import java.net.URLEncoder; | ||||||
|
|
||||||
| import org.eclipse.e4.ui.di.Focus; | ||||||
| import org.eclipse.jface.fieldassist.ContentProposalAdapter; | ||||||
| import org.eclipse.jface.fieldassist.SimpleContentProposalProvider; | ||||||
| import org.eclipse.jface.fieldassist.TextContentAdapter; | ||||||
| import org.eclipse.nebula.widgets.chips.Chips; | ||||||
| import org.eclipse.swt.SWT; | ||||||
| import org.eclipse.swt.browser.Browser; | ||||||
| import org.eclipse.swt.graphics.Image; | ||||||
| import org.eclipse.swt.events.SelectionListener; | ||||||
| import org.eclipse.swt.graphics.Point; | ||||||
| import org.eclipse.swt.layout.GridLayout; | ||||||
| import org.eclipse.swt.widgets.Composite; | ||||||
| import org.eclipse.swt.widgets.Label; | ||||||
| import org.eclipse.swt.widgets.Display; | ||||||
| import org.eclipse.swt.widgets.Text; | ||||||
|
|
||||||
| import com.vogella.imageloader.services.IBundleResourceLoader; | ||||||
|
|
||||||
| import jakarta.annotation.PostConstruct; | ||||||
| import jakarta.inject.Inject; | ||||||
|
|
||||||
| public class PlaygroundPart { | ||||||
| private Text text; | ||||||
| private Browser browser; | ||||||
| private Text target; | ||||||
|
|
||||||
|
|
||||||
| @Inject | ||||||
| MPart part; | ||||||
| @Inject | ||||||
| IBundleResourceLoader loader; | ||||||
|
|
||||||
| @PostConstruct | ||||||
| public void createControls(Composite parent) { | ||||||
| parent.setLayout(new GridLayout(2, false)); | ||||||
| Chips chip1 = new Chips(parent, SWT.CLOSE); | ||||||
| chip1.setText("Example"); | ||||||
| chip1.setChipsBackground(Display.getDefault().getSystemColor(SWT.COLOR_RED)); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's generally safer to get the
Suggested change
|
||||||
| text = text(SWT.BORDER | SWT.SEARCH | SWT.ICON_SEARCH | SWT.ICON_CANCEL).message("Enter City") | ||||||
| .layoutData(fillDefaults().grab(true, false).create()).create(parent); | ||||||
| text.addSelectionListener(SelectionListener.widgetDefaultSelectedAdapter(e -> updateBrowser())); | ||||||
|
|
||||||
| ContentProposalAdapter contentProposal = new ContentProposalAdapter(text, new TextContentAdapter(), | ||||||
| new SimpleContentProposalProvider("Hamburg", "New York", "New Delhi"), null, null); | ||||||
|
|
||||||
| Label label = new Label(parent, SWT.NONE); | ||||||
|
|
||||||
| // the following code assumes that you have a vogella.png file | ||||||
| // in a folder called "images" in this plug-in | ||||||
| ResourceManager resourceManager = | ||||||
| new LocalResourceManager(JFaceResources.getResources(), label); | ||||||
| Image image = resourceManager. | ||||||
| create(loader.getImageDescriptor(this.getClass(), "images/sbahn.svg")); | ||||||
| label.setImage(image); | ||||||
|
|
||||||
| contentProposal.setPopupSize(new Point(200, 100)); | ||||||
| contentProposal.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE); | ||||||
| button(SWT.PUSH).text("Search").onSelect(e -> updateBrowser()).create(parent); | ||||||
|
|
||||||
| browser = new Browser(parent, SWT.NONE); | ||||||
| browser.setLayoutData(fillDefaults().grab(true, true).span(2, 1).create()); | ||||||
| } | ||||||
|
|
||||||
| @Persist | ||||||
| public void saveItReallyReally() { | ||||||
| // TODO really do the saving | ||||||
| part.setDirty(false); | ||||||
| private void updateBrowser() { | ||||||
| String city = text.getText(); | ||||||
| if (city.isEmpty()) { | ||||||
| return; | ||||||
| } | ||||||
| try { | ||||||
| browser.setUrl("https://www.google.com/maps/place/" + URLEncoder.encode(city, "UTF-8") + "/&output=embed"); | ||||||
|
|
||||||
| } catch (UnsupportedEncodingException e1) { | ||||||
| e1.printStackTrace(); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using // Log the exception properly or display a user-friendly error
// Logger.error("Failed to encode city name for URL", e1);
// MessageDialog.openError(parent.getShell(), "Encoding Error", "Failed to encode city name."); |
||||||
| } | ||||||
|
Comment on lines
+56
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The browser.setUrl("https://www.google.com/maps/place/" + URLEncoder.encode(city, java.nio.charset.StandardCharsets.UTF_8) + "/&output=embed"); |
||||||
| } | ||||||
|
|
||||||
| @Inject | ||||||
| public void getFromOSGi(@Optional @EventTopic("YOURKEY") String value) { | ||||||
| System.out.println(value); | ||||||
| @Focus | ||||||
| public void onFocus() { | ||||||
| text.setFocus(); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,21 @@ | ||
| package com.vogella.tasks.ui.toolcontrols; | ||
|
|
||
| import jakarta.annotation.PostConstruct; | ||
|
|
||
| import org.eclipse.jface.layout.GridDataFactory; | ||
| import org.eclipse.swt.SWT; | ||
| import org.eclipse.swt.layout.GridLayout; | ||
| import org.eclipse.swt.widgets.Composite; | ||
| import org.eclipse.swt.widgets.Text; | ||
|
|
||
| import jakarta.annotation.PostConstruct; | ||
|
|
||
| public class SearchToolControl { | ||
|
|
||
| @PostConstruct | ||
| public void createGui(Composite parent) { | ||
| Text text = new Text(parent, SWT.SEARCH | SWT.CANCEL | SWT.BORDER); | ||
| final Composite comp = new Composite(parent, SWT.NONE); | ||
| comp.setLayout(new GridLayout()); | ||
| Text text = new Text(comp, SWT.SEARCH | SWT.CANCEL | SWT.ICON_SEARCH | SWT.BORDER); | ||
| text.setMessage("Search"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| GridDataFactory.fillDefaults().hint(130, SWT.DEFAULT).applyTo(text); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
id="id1"for the extension is generic. It's a good practice to use more descriptive IDs to prevent potential conflicts and improve readability, especially in larger applications or when integrating with other plugins. Consider using a naming convention likecom.vogella.tasks.ui.contribute.menuprocessor.extension.