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,40 @@ 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
96+ .setText (Messages .IDFConsoleWizardConfigurationPanel_IDFConsoleWizardConfigurationPanel_ProjectLabel );
97+
98+ projectCombo = new Combo (panel , SWT .READ_ONLY );
99+ projectCombo .setLayoutData (new GridData (SWT .FILL , SWT .CENTER , true , false ));
100+
101+ for (IProject project : ResourcesPlugin .getWorkspace ().getRoot ().getProjects ()) {
102+ try {
103+ if (project .hasNature (IDFProjectNature .ID )) {
104+ projectCombo .add (project .getName ());
105+ }
106+ } catch (CoreException e ) {
107+ Logger .log (e );
108+ }
82109 }
83110
84- setControl (panel );
111+ Optional <IProject > optProject = Optional .ofNullable (EclipseUtil .getSelectedIDFProjectInExplorer ());
112+ optProject .ifPresentOrElse (project -> projectCombo .setText (project .getName ()), () -> {
113+ if (projectCombo .getItemCount () > 0 )
114+ projectCombo .select (0 );
115+ });
85116 }
86117
87118 @ Override
@@ -96,19 +127,17 @@ public void setupData(Map<String, Object> data) {
96127
97128 @ Override
98129 public void extractData (Map <String , Object > data ) {
99- // set the terminal connector id for local terminal
130+
100131 data .put (ITerminalsConnectorConstants .PROP_TERMINAL_CONNECTOR_ID ,
101132 "com.espressif.idf.terminal.connector.espidfConnector" ); //$NON-NLS-1$
102133
103- // Store the encoding
104134 data .put (ITerminalsConnectorConstants .PROP_ENCODING , getEncoding ());
105135
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 );
136+ if (projectCombo != null && !projectCombo .isDisposed () && !projectCombo .getText ().isEmpty ()) {
137+ IProject p = ResourcesPlugin .getWorkspace ().getRoot ().getProject (projectCombo .getText ());
138+ if (p != null && p .exists () && p .getLocation () != null ) {
139+ data .put (ITerminalsConnectorConstants .PROP_PROCESS_WORKING_DIR , p .getLocation ().toOSString ());
140+ data .put (ITerminalsConnectorConstants .PROP_TITLE , p .getName ());
112141 }
113142 }
114143 }
@@ -147,25 +176,4 @@ protected String getHostFromSettings() {
147176 public boolean isWithHostList () {
148177 return false ;
149178 }
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- }
171179}
0 commit comments