1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-25 18:05:33 +02:00

Bug 491192 - Add SerialPortConnectionPropertyPage

Change-Id: If95f04b716e7e861bfebc010ba2d580457d4e5dd
Signed-off-by: Jonathan Williams <jonwilliams@qnx.com>
This commit is contained in:
Jonathan Williams 2016-04-06 17:28:18 -04:00
parent eb4246980a
commit d68f0e3881
7 changed files with 417 additions and 161 deletions

View file

@ -10,9 +10,11 @@ Bundle-Vendor: %providerName
Bundle-Localization: plugin
Export-Package: org.eclipse.remote.serial.ui
Import-Package: org.eclipse.cdt.serial,
org.eclipse.core.expressions,
org.eclipse.core.runtime,
org.eclipse.jface.dialogs,
org.eclipse.jface.operation,
org.eclipse.jface.preference,
org.eclipse.jface.resource,
org.eclipse.jface.viewers,
org.eclipse.jface.window,
@ -27,5 +29,7 @@ Import-Package: org.eclipse.cdt.serial,
org.eclipse.swt.graphics,
org.eclipse.swt.layout,
org.eclipse.swt.widgets,
org.eclipse.ui,
org.eclipse.ui.dialogs,
org.eclipse.ui.plugin,
org.osgi.framework

View file

@ -9,5 +9,31 @@
service="org.eclipse.remote.ui.IRemoteUIConnectionService">
</connectionTypeService>
</extension>
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="org.eclipse.remote.serial.internal.ui.SerialPortConnectionPropertyTester"
id="remoteTester"
namespace="org.eclipse.remote.serial.ui"
properties="isSerialRemote"
type="org.eclipse.remote.core.IRemoteConnection">
</propertyTester>
</extension>
<extension
point="org.eclipse.ui.propertyPages">
<page
class="org.eclipse.remote.serial.ui.SerialPortConnectionPropertyPage"
id="org.eclipse.remote.serial.ui.targetPropertyPage"
name="Serial Port Settings"
selectionFilter="single">
<enabledWhen>
<adapt type="org.eclipse.remote.core.IRemoteConnection">
<test
forcePluginActivation="false"
property="org.eclipse.remote.serial.ui.isSerialRemote">
</test>
</adapt>
</enabledWhen>
</page>
</extension>
</plugin>

View file

@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2016 QNX Software Systems, 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:
* QNX Software Systems - initial contribution
*******************************************************************************/
package org.eclipse.remote.serial.internal.ui;
import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.remote.core.IRemoteConnection;
import org.eclipse.remote.serial.core.ISerialPortService;
public class SerialPortConnectionPropertyTester extends PropertyTester {
@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
if (receiver instanceof IRemoteConnection) {
IRemoteConnection remote = (IRemoteConnection) receiver;
return remote.hasService(ISerialPortService.class);
} else {
return false;
}
}
}

View file

@ -69,7 +69,7 @@ public class NewSerialPortConnectionWizard extends Wizard implements IRemoteUICo
public IRemoteConnectionWorkingCopy getConnection() {
if (workingCopy == null) {
try {
workingCopy = connectionType.newConnection(page.getName());
workingCopy = connectionType.newConnection(page.getConnectionName());
} catch (RemoteConnectionException e) {
Activator.log(e.getStatus());
}

View file

@ -10,200 +10,61 @@
*******************************************************************************/
package org.eclipse.remote.serial.ui;
import java.io.IOException;
import org.eclipse.cdt.serial.BaudRate;
import org.eclipse.cdt.serial.ByteSize;
import org.eclipse.cdt.serial.Parity;
import org.eclipse.cdt.serial.SerialPort;
import org.eclipse.cdt.serial.StopBits;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.remote.serial.internal.ui.Activator;
import org.eclipse.remote.serial.internal.ui.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
public class NewSerialPortConnectionWizardPage extends WizardPage {
private String name;
private String portName;
private int baudRateIndex;
private int byteSizeIndex;
private int parityIndex;
private int stopBitsIndex;
private String[] portNames;
private Text nameText;
private Combo portCombo;
private Combo baudRateCombo;
private Combo byteSizeCombo;
private Combo parityCombo;
private Combo stopBitsCombo;
SerialPortConnectionBlock block;
public NewSerialPortConnectionWizardPage() {
super(NewSerialPortConnectionWizardPage.class.getName());
setDescription(Messages.NewSerialPortConnectionWizardPage_Description);
setTitle(Messages.NewSerialPortConnectionWizardPage_Title);
block = new SerialPortConnectionBlock();
block.addUpdateListener(block.new SerialBlockUpdateListener() {
@Override
public void update() {
setPageComplete(block.isComplete());
}
});
}
@Override
public void createControl(Composite parent) {
Composite comp = new Composite(parent, SWT.NONE);
comp.setLayout(new GridLayout(2, false));
Label nameLabel = new Label(comp, SWT.NONE);
nameLabel.setText(Messages.NewSerialPortConnectionWizardPage_NameLabel);
nameText = new Text(comp, SWT.BORDER | SWT.SINGLE);
nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
nameText.setText(""); //$NON-NLS-1$
nameText.addKeyListener(new KeyListener() {
@Override
public void keyReleased(KeyEvent e) {
updateStatus();
}
@Override
public void keyPressed(KeyEvent e) {
}
});
Label portLabel = new Label(comp, SWT.NONE);
portLabel.setText(Messages.NewSerialPortConnectionWizardPage_PortLabel);
portCombo = new Combo(comp, SWT.READ_ONLY);
portCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
try {
portNames = SerialPort.list();
} catch (IOException e) {
Activator.log(e);
}
for (String portName : portNames) {
portCombo.add(portName);
}
portCombo.select(0);
portCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateStatus();
}
});
Label baudRateLabel = new Label(comp, SWT.NONE);
baudRateLabel.setText(Messages.NewSerialPortConnectionWizardPage_BaudRateLabel);
baudRateCombo = new Combo(comp, SWT.READ_ONLY);
baudRateCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
for (String baudRateStr : BaudRate.getStrings()) {
baudRateCombo.add(baudRateStr);
}
// TODO remember the last one
baudRateCombo.select(BaudRate.getStringIndex(BaudRate.getDefault()));
baudRateCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateStatus();
}
});
Label byteSizeLabel = new Label(comp, SWT.NONE);
byteSizeLabel.setText(Messages.NewSerialPortConnectionWizardPage_ByteSizeLabel);
byteSizeCombo = new Combo(comp, SWT.READ_ONLY);
byteSizeCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
for (String byteSizeStr : ByteSize.getStrings()) {
byteSizeCombo.add(byteSizeStr);
}
byteSizeCombo.select(ByteSize.getStringIndex(ByteSize.getDefault()));
byteSizeCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateStatus();
}
});
Label parityLabel = new Label(comp, SWT.NONE);
parityLabel.setText(Messages.NewSerialPortConnectionWizardPage_ParityLabel);
parityCombo = new Combo(comp, SWT.READ_ONLY);
parityCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
for (String parityStr : Parity.getStrings()) {
parityCombo.add(parityStr);
}
parityCombo.select(Parity.getStringIndex(Parity.getDefault()));
parityCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateStatus();
}
});
Label stopBitsLabel = new Label(comp, SWT.NONE);
stopBitsLabel.setText(Messages.NewSerialPortConnectionWizardPage_StopBitsLabel);
stopBitsCombo = new Combo(comp, SWT.READ_ONLY);
stopBitsCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
for (String stopBitsStr : StopBits.getStrings()) {
stopBitsCombo.add(stopBitsStr);
}
stopBitsCombo.select(StopBits.getStringIndex(StopBits.getDefault()));
stopBitsCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateStatus();
}
});
comp.setLayout(new GridLayout(2, false));
block.createBlock(comp, null);
setControl(comp);
updateStatus();
}
private void updateStatus() {
name = nameText.getText();
int portIndex = portCombo.getSelectionIndex();
portName = portIndex < 0 ? null : portNames[portIndex];
baudRateIndex = baudRateCombo.getSelectionIndex();
byteSizeIndex = byteSizeCombo.getSelectionIndex();
parityIndex = parityCombo.getSelectionIndex();
stopBitsIndex = stopBitsCombo.getSelectionIndex();
setPageComplete(!name.isEmpty() && portName != null);
}
public String getName() {
return name;
public String getConnectionName() {
return block.getConnectionName();
}
public String getPortName() {
return portName;
return block.getPortName();
}
public int getBaudRateIndex() {
return baudRateIndex;
return block.getBaudRateIndex();
}
public int getByteSizeIndex() {
return byteSizeIndex;
return block.getByteSizeIndex();
}
public int getParityIndex() {
return parityIndex;
return block.getParityIndex();
}
public int getStopBitsIndex() {
return stopBitsIndex;
return block.getStopBitsIndex();
}
}

View file

@ -0,0 +1,253 @@
/*******************************************************************************
* Copyright (c) 2016 QNX Software Systems, 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:
* QNX Software Systems - initial contribution
*******************************************************************************/
package org.eclipse.remote.serial.ui;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.cdt.serial.BaudRate;
import org.eclipse.cdt.serial.ByteSize;
import org.eclipse.cdt.serial.Parity;
import org.eclipse.cdt.serial.SerialPort;
import org.eclipse.cdt.serial.StopBits;
import org.eclipse.remote.core.IRemoteConnectionWorkingCopy;
import org.eclipse.remote.serial.core.ISerialPortService;
import org.eclipse.remote.serial.internal.ui.Activator;
import org.eclipse.remote.serial.internal.ui.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
public class SerialPortConnectionBlock {
private String name;
private String portName;
private int baudRateIndex;
private int byteSizeIndex;
private int parityIndex;
private int stopBitsIndex;
private String[] portNames;
private Text nameText;
private Combo portCombo;
private Combo baudRateCombo;
private Combo byteSizeCombo;
private Combo parityCombo;
private Combo stopBitsCombo;
private boolean isComplete;
private List<SerialBlockUpdateListener> listeners = new ArrayList<>();
/**
* Creates the UI elements for the SerialPortConnectionBlock
*
* @param comp - parent composite
* @param wc - an IRemoteConnectionWorkingCopy to populate the default values from. Can be null.
*/
public void createBlock(Composite comp, IRemoteConnectionWorkingCopy wc) {
String name = "";
String connectionPortName = "";
int baudRateStringIndex = BaudRate.getStringIndex(BaudRate.getDefault());
int byteSizeStringIndex = ByteSize.getStringIndex(ByteSize.getDefault());
int parityStringIndex = Parity.getStringIndex(Parity.getDefault());
int stopBitsStringIndex = StopBits.getStringIndex(StopBits.getDefault());
if (wc != null) {
name = wc.getName();
connectionPortName = wc.getAttribute(ISerialPortService.PORT_NAME_ATTR);
baudRateStringIndex = Integer.parseInt(wc.getAttribute(ISerialPortService.BAUD_RATE_ATTR));
byteSizeStringIndex = Integer.parseInt(wc.getAttribute(ISerialPortService.BYTE_SIZE_ATTR));
parityStringIndex = Integer.parseInt(wc.getAttribute(ISerialPortService.PARITY_ATTR));
stopBitsStringIndex = Integer.parseInt(wc.getAttribute(ISerialPortService.STOP_BITS_ATTR));
}
Label nameLabel = new Label(comp, SWT.NONE);
nameLabel.setText(Messages.NewSerialPortConnectionWizardPage_NameLabel);
nameText = new Text(comp, SWT.BORDER | SWT.SINGLE);
nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
nameText.setText(name); //$NON-NLS-1$
nameText.addKeyListener(new KeyListener() {
@Override
public void keyReleased(KeyEvent e) {
updateStatus();
}
@Override
public void keyPressed(KeyEvent e) {
}
});
Label portLabel = new Label(comp, SWT.NONE);
portLabel.setText(Messages.NewSerialPortConnectionWizardPage_PortLabel);
portCombo = new Combo(comp, SWT.READ_ONLY);
portCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
try {
portNames = SerialPort.list();
} catch (IOException e) {
Activator.log(e);
}
int index = 0;
int portNameIndex = 0;
for (String portName : portNames) {
portCombo.add(portName);
if (portName.equals(connectionPortName))
portNameIndex = index;
index++;
}
portCombo.select(portNameIndex);
portCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateStatus();
}
});
Label baudRateLabel = new Label(comp, SWT.NONE);
baudRateLabel.setText(Messages.NewSerialPortConnectionWizardPage_BaudRateLabel);
baudRateCombo = new Combo(comp, SWT.READ_ONLY);
baudRateCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
for (String baudRateStr : BaudRate.getStrings()) {
baudRateCombo.add(baudRateStr);
}
baudRateCombo.select(baudRateStringIndex);
baudRateCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateStatus();
}
});
Label byteSizeLabel = new Label(comp, SWT.NONE);
byteSizeLabel.setText(Messages.NewSerialPortConnectionWizardPage_ByteSizeLabel);
byteSizeCombo = new Combo(comp, SWT.READ_ONLY);
byteSizeCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
for (String byteSizeStr : ByteSize.getStrings()) {
byteSizeCombo.add(byteSizeStr);
}
byteSizeCombo.select(byteSizeStringIndex);
byteSizeCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateStatus();
}
});
Label parityLabel = new Label(comp, SWT.NONE);
parityLabel.setText(Messages.NewSerialPortConnectionWizardPage_ParityLabel);
parityCombo = new Combo(comp, SWT.READ_ONLY);
parityCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
for (String parityStr : Parity.getStrings()) {
parityCombo.add(parityStr);
}
parityCombo.select(parityStringIndex);
parityCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateStatus();
}
});
Label stopBitsLabel = new Label(comp, SWT.NONE);
stopBitsLabel.setText(Messages.NewSerialPortConnectionWizardPage_StopBitsLabel);
stopBitsCombo = new Combo(comp, SWT.READ_ONLY);
stopBitsCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
for (String stopBitsStr : StopBits.getStrings()) {
stopBitsCombo.add(stopBitsStr);
}
stopBitsCombo.select(stopBitsStringIndex);
stopBitsCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateStatus();
}
});
updateStatus();
}
private void updateStatus() {
name = nameText.getText();
int portIndex = portCombo.getSelectionIndex();
portName = portIndex < 0 ? null : portNames[portIndex];
baudRateIndex = baudRateCombo.getSelectionIndex();
byteSizeIndex = byteSizeCombo.getSelectionIndex();
parityIndex = parityCombo.getSelectionIndex();
stopBitsIndex = stopBitsCombo.getSelectionIndex();
isComplete = (!name.isEmpty() && portName != null);
for(SerialBlockUpdateListener listener : listeners) {
listener.update();
}
}
public String getConnectionName() {
return name;
}
public String getPortName() {
return portName;
}
public int getBaudRateIndex() {
return baudRateIndex;
}
public int getByteSizeIndex() {
return byteSizeIndex;
}
public int getParityIndex() {
return parityIndex;
}
public int getStopBitsIndex() {
return stopBitsIndex;
}
public boolean isComplete() {
return isComplete;
}
public void addUpdateListener(SerialBlockUpdateListener listener) {
if (listener != null && !listeners.contains(listener))
listeners.add(listener);
}
public void removeUpdateListener(SerialBlockUpdateListener listener) {
listeners.remove(listener);
}
public abstract class SerialBlockUpdateListener {
public abstract void update();
}
}

View file

@ -0,0 +1,83 @@
/*******************************************************************************
* Copyright (c) 2016 QNX Software Systems, 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:
* QNX Software Systems - initial contribution
*******************************************************************************/
package org.eclipse.remote.serial.ui;
import org.eclipse.remote.core.IRemoteConnection;
import org.eclipse.remote.core.IRemoteConnectionWorkingCopy;
import org.eclipse.remote.core.exception.RemoteConnectionException;
import org.eclipse.remote.serial.core.ISerialPortService;
import org.eclipse.remote.serial.internal.ui.Activator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IWorkbenchPropertyPage;
import org.eclipse.ui.dialogs.PropertyPage;
public class SerialPortConnectionPropertyPage extends PropertyPage implements IWorkbenchPropertyPage {
private SerialPortConnectionBlock block;
private IRemoteConnectionWorkingCopy workingCopy;
public SerialPortConnectionPropertyPage() {
super();
block = new SerialPortConnectionBlock();
}
@Override
protected Control createContents(Composite parent) {
Composite comp = new Composite(parent, SWT.NONE);
comp.setLayout(new GridLayout(2, false));
block.addUpdateListener(block.new SerialBlockUpdateListener() {
@Override
public void update() {
setValid(block.isComplete());
}
});
IRemoteConnection remoteConnection = getElement().getAdapter(IRemoteConnection.class);
if (remoteConnection != null)
workingCopy = remoteConnection.getWorkingCopy();
else
workingCopy = null;
block.createBlock(comp, workingCopy);
return comp;
}
@Override
public boolean performOk() {
if (workingCopy != null) {
workingCopy.setName(block.getConnectionName());
workingCopy.setAttribute(ISerialPortService.PORT_NAME_ATTR, block.getPortName());
workingCopy.setAttribute(ISerialPortService.BAUD_RATE_ATTR, Integer.toString(block.getBaudRateIndex()));
workingCopy.setAttribute(ISerialPortService.BYTE_SIZE_ATTR, Integer.toString(block.getByteSizeIndex()));
workingCopy.setAttribute(ISerialPortService.PARITY_ATTR, Integer.toString(block.getParityIndex()));
workingCopy.setAttribute(ISerialPortService.STOP_BITS_ATTR, Integer.toString(block.getStopBitsIndex()));
try {
workingCopy.save();
} catch (RemoteConnectionException e) {
Activator.log(e);
return false;
}
}
return true;
}
}