1313package com .espressif .idf .terminal .connector .controls ;
1414
1515import java .util .Map ;
16+ import java .util .Optional ;
1617
17- import org .eclipse .core .runtime .IAdaptable ;
18+ import org .eclipse .core .resources .IProject ;
19+ import org .eclipse .core .resources .ResourcesPlugin ;
20+ import org .eclipse .core .runtime .CoreException ;
1821import org .eclipse .core .runtime .Platform ;
1922import org .eclipse .jface .dialogs .IDialogSettings ;
20- import org .eclipse .jface .viewers .ISelection ;
21- import org .eclipse .jface .viewers .IStructuredSelection ;
22- import org .eclipse .jface .viewers .StructuredSelection ;
2323import org .eclipse .swt .SWT ;
2424import org .eclipse .swt .layout .GridData ;
2525import org .eclipse .swt .layout .GridLayout ;
26+ import org .eclipse .swt .widgets .Combo ;
2627import org .eclipse .swt .widgets .Composite ;
2728import org .eclipse .swt .widgets .Label ;
2829import org .eclipse .tm .terminal .view .core .interfaces .constants .ITerminalsConnectorConstants ;
2930import org .eclipse .tm .terminal .view .ui .interfaces .IConfigurationPanelContainer ;
3031import org .eclipse .tm .terminal .view .ui .panels .AbstractExtendedConfigurationPanel ;
31- import org .eclipse .ui .ISelectionService ;
32- import org .eclipse .ui .PlatformUI ;
3332import org .eclipse .ui .WorkbenchEncoding ;
34- import org .osgi .framework .Bundle ;
33+
34+ import com .espressif .idf .core .IDFProjectNature ;
35+ import com .espressif .idf .core .logging .Logger ;
36+ import com .espressif .idf .ui .EclipseUtil ;
3537
3638/**
3739 * IDF console wizard configuration panel implementation.
3840 */
3941public class IDFConsoleWizardConfigurationPanel extends AbstractExtendedConfigurationPanel {
4042
41- private Object resource ;
43+ private Combo projectCombo ;
4244
4345 /**
4446 * Constructor.
@@ -55,6 +57,7 @@ public void setupPanel(Composite parent) {
5557 panel .setLayout (new GridLayout ());
5658 panel .setLayoutData (new GridData (SWT .FILL , SWT .FILL , true , true ));
5759
60+ createProjectCombo (panel );
5861 // Create the encoding selection combo
5962 createEncodingUI (panel , false );
6063
@@ -76,12 +79,36 @@ public void setupPanel(Composite parent) {
7679 layoutData .heightHint = 80 ;
7780 label .setLayoutData (layoutData );
7881
79- Bundle bundle = Platform .getBundle ("org.eclipse.core.resources" ); //$NON-NLS-1$
80- if (bundle != null && bundle .getState () != Bundle .UNINSTALLED && bundle .getState () != Bundle .STOPPING ) {
81- resource = getSelectionResource ();
82+ setControl (panel );
83+ }
84+
85+ private void createProjectCombo (Composite parent ) {
86+
87+ Composite panel = new Composite (parent , SWT .NONE );
88+ GridLayout layout = new GridLayout (2 , false );
89+ layout .marginHeight = 0 ;
90+ layout .marginWidth = 0 ;
91+ panel .setLayout (layout );
92+ panel .setLayoutData (new GridData (SWT .FILL , SWT .CENTER , true , false ));
93+
94+ Label projectLabel = new Label (panel , SWT .NONE );
95+ projectLabel .setText ("Project name:" );
96+
97+ projectCombo = new Combo (panel , SWT .READ_ONLY );
98+ projectCombo .setLayoutData (new GridData (SWT .FILL , SWT .CENTER , true , false ));
99+
100+ for (IProject project : ResourcesPlugin .getWorkspace ().getRoot ().getProjects ()) {
101+ try {
102+ if (project .hasNature (IDFProjectNature .ID )) {
103+ projectCombo .add (project .getName ());
104+ }
105+ } catch (CoreException e ) {
106+ Logger .log (e );
107+ }
82108 }
83109
84- setControl (panel );
110+ Optional <IProject > optProject = Optional .ofNullable (EclipseUtil .getSelectedIDFProjectInExplorer ());
111+ optProject .ifPresentOrElse (project -> projectCombo .setText (project .getName ()), () -> projectCombo .select (0 ));
85112 }
86113
87114 @ Override
@@ -96,19 +123,18 @@ public void setupData(Map<String, Object> data) {
96123
97124 @ Override
98125 public void extractData (Map <String , Object > data ) {
99- // set the terminal connector id for local terminal
126+
100127 data .put (ITerminalsConnectorConstants .PROP_TERMINAL_CONNECTOR_ID ,
101128 "com.espressif.idf.terminal.connector.espidfConnector" ); //$NON-NLS-1$
102129
103- // Store the encoding
104130 data .put (ITerminalsConnectorConstants .PROP_ENCODING , getEncoding ());
105131
106- Bundle bundle = Platform . getBundle ( "org.eclipse.core.resources" ); //$NON-NLS-1$
107- if (bundle != null && bundle . getState () != Bundle . UNINSTALLED && bundle . getState () != Bundle . STOPPING ) {
108- // if we have a IResource selection use the location for working directory
109- if (resource instanceof org . eclipse . core . resources . IResource ) {
110- String dir = (( org . eclipse . core . resources . IResource ) resource ). getProject (). getLocation ().toString ( );
111- data .put (ITerminalsConnectorConstants .PROP_PROCESS_WORKING_DIR , dir );
132+ // <-- this is the important part
133+ if (projectCombo != null && ! projectCombo . isDisposed () ) {
134+ IProject p = ResourcesPlugin . getWorkspace (). getRoot (). getProject ( projectCombo . getText ());
135+ if (p != null && p . exists () && p . getLocation () != null ) {
136+ data . put ( ITerminalsConnectorConstants . PROP_PROCESS_WORKING_DIR , p . getLocation ().toOSString () );
137+ data .put (ITerminalsConnectorConstants .PROP_TITLE , p . getName () );
112138 }
113139 }
114140 }
@@ -147,25 +173,4 @@ protected String getHostFromSettings() {
147173 public boolean isWithHostList () {
148174 return false ;
149175 }
150-
151- /**
152- * Returns the IResource from the current selection
153- *
154- * @return the IResource, or <code>null</code>.
155- */
156- private org .eclipse .core .resources .IResource getSelectionResource () {
157- ISelectionService selectionService = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getSelectionService ();
158- ISelection selection = selectionService != null ? selectionService .getSelection () : StructuredSelection .EMPTY ;
159-
160- if (selection instanceof IStructuredSelection && !selection .isEmpty ()) {
161- Object element = ((IStructuredSelection ) selection ).getFirstElement ();
162- if (element instanceof org .eclipse .core .resources .IResource ) {
163- return ((org .eclipse .core .resources .IResource ) element );
164- }
165- if (element instanceof IAdaptable ) {
166- return ((IAdaptable ) element ).getAdapter (org .eclipse .core .resources .IResource .class );
167- }
168- }
169- return null ;
170- }
171176}
0 commit comments