Home » geronimo-eclipse-plugin-2.2-source-release » org.apache.geronimo.st.ui.internal » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *    http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.geronimo.st.ui.internal;
   18   
   19   import org.apache.geronimo.st.core.GeronimoServerDelegate;
   20   import org.apache.geronimo.st.ui.Activator;
   21   import org.eclipse.jface.viewers.CellEditor;
   22   import org.eclipse.jface.viewers.ColumnWeightData;
   23   import org.eclipse.jface.viewers.ICellModifier;
   24   import org.eclipse.jface.viewers.TableLayout;
   25   import org.eclipse.jface.viewers.TableViewer;
   26   import org.eclipse.jface.viewers.TextCellEditor;
   27   import org.eclipse.swt.SWT;
   28   import org.eclipse.swt.events.ModifyEvent;
   29   import org.eclipse.swt.events.ModifyListener;
   30   import org.eclipse.swt.layout.GridData;
   31   import org.eclipse.swt.layout.GridLayout;
   32   import org.eclipse.swt.widgets.Composite;
   33   import org.eclipse.swt.widgets.Item;
   34   import org.eclipse.swt.widgets.Label;
   35   import org.eclipse.swt.widgets.Table;
   36   import org.eclipse.swt.widgets.TableColumn;
   37   import org.eclipse.swt.widgets.TableItem;
   38   import org.eclipse.swt.widgets.Text;
   39   import org.eclipse.wst.server.core.IServerWorkingCopy;
   40   import org.eclipse.wst.server.core.ServerPort;
   41   import org.eclipse.wst.server.core.TaskModel;
   42   import org.eclipse.wst.server.ui.wizard.IWizardHandle;
   43   import org.eclipse.wst.server.ui.wizard.WizardFragment;
   44   
   45   /**
   46    * @version $Rev: 471551 $ $Date: 2006-11-06 06:47:11 +0800 (Mon, 06 Nov 2006) $
   47    */
   48   public class GeronimoServerWizardFragment extends WizardFragment {
   49   
   50   	protected Text hostName;
   51   
   52   	protected Text adminId;
   53   
   54   	protected Text password;
   55   
   56   	protected Table ports;
   57   
   58   	protected TableViewer viewer;
   59   
   60   	/* (non-Javadoc)
   61   	 * @see org.eclipse.wst.server.ui.wizard.WizardFragment#hasComposite()
   62   	 */
   63   	public boolean hasComposite() {
   64   		return true;
   65   	}
   66   
   67   	public Composite createComposite(Composite parent, IWizardHandle handle) {
   68   		Composite container = new Composite(parent, SWT.NONE);
   69   		GridLayout grid = new GridLayout(2, false);
   70   		grid.marginWidth = 0;
   71   		container.setLayout(grid);
   72   		container.setLayoutData(new GridData(GridData.FILL_BOTH));
   73   		handle.setImageDescriptor(Activator.getImageDescriptor(Activator.IMG_WIZ_GERONIMO));
   74   		handle.setTitle(Messages.bind(Messages.newServerWizardTitle, getServerName()));
   75   		handle.setDescription(Messages.bind(Messages.newServerWizardTitle, getServerName()));
   76   		createContent(container, handle);
   77   		return container;
   78   	}
   79   
   80   	public void createContent(Composite parent, IWizardHandle handle) {
   81   		Label label = new Label(parent, SWT.NONE);
   82   		label.setText(Messages.hostName);
   83   		label.setLayoutData(new GridData());
   84   
   85   		hostName = new Text(parent, SWT.BORDER);
   86   		hostName.setLayoutData(createTextGridData());
   87   		hostName.setText(getServer().getHost());
   88   
   89   		hostName.addModifyListener(new ModifyListener() {
   90   			public void modifyText(ModifyEvent e) {
   91   				getServer().setHost(hostName.getText());
   92   			}
   93   		});
   94   
   95   		label = new Label(parent, SWT.NONE);
   96   		label.setText(Messages.adminId);
   97   		label.setLayoutData(new GridData());
   98   
   99   		adminId = new Text(parent, SWT.BORDER);
  100   		adminId.setLayoutData(createTextGridData());
  101   		adminId.setText(getGeronimoServer().getAdminID());
  102   
  103   		adminId.addModifyListener(new ModifyListener() {
  104   			public void modifyText(ModifyEvent e) {
  105   				getGeronimoServer().setAdminID(adminId.getText());
  106   			}
  107   		});
  108   
  109   		label = new Label(parent, SWT.NONE);
  110   		label.setText(Messages.adminPassword);
  111   		label.setLayoutData(new GridData());
  112   
  113   		password = new Text(parent, SWT.BORDER | SWT.PASSWORD);
  114   		password.setLayoutData(createTextGridData());
  115   		password.setText(getGeronimoServer().getAdminPassword());
  116   
  117   		password.addModifyListener(new ModifyListener() {
  118   			public void modifyText(ModifyEvent e) {
  119   				getGeronimoServer().setAdminPassword(password.getText());
  120   			}
  121   		});
  122   
  123   		label = new Label(parent, SWT.NONE);
  124   		label.setText(Messages.specifyPorts);
  125   		GridData data = createTextGridData();
  126   		data.verticalIndent = 15;
  127   		data.horizontalSpan = 2;
  128   		label.setLayoutData(data);
  129   
  130   		// ports
  131   		ports = new Table(parent, SWT.V_SCROLL | SWT.H_SCROLL
  132   				| SWT.FULL_SELECTION);
  133   		ports.setHeaderVisible(true);
  134   		ports.setLinesVisible(false);
  135   
  136   		TableColumn col = new TableColumn(ports, SWT.NONE);
  137   		col.setText(Messages.portName);
  138   		col.setResizable(false);
  139   		ColumnWeightData colData = new ColumnWeightData(15, 150, false);
  140   		TableLayout tableLayout = new TableLayout();
  141   		tableLayout.addColumnData(colData);
  142   
  143   		col = new TableColumn(ports, SWT.NONE);
  144   		col.setText(Messages.portValue);
  145   		col.setResizable(false);
  146   		colData = new ColumnWeightData(8, 80, false);
  147   		tableLayout.addColumnData(colData);
  148   
  149   		data = new GridData(GridData.FILL_HORIZONTAL
  150   				| GridData.VERTICAL_ALIGN_FILL);
  151   		data.horizontalSpan = 2;
  152   		data.heightHint = 100;
  153   		ports.setLayoutData(data);
  154   		ports.setLayout(tableLayout);
  155   
  156   		viewer = new TableViewer(ports);
  157   		viewer.setColumnProperties(new String[] { "name", "port" });
  158   
  159   		fillTable(ports);
  160   		addCellEditor(ports);
  161   	}
  162   
  163   	private void addCellEditor(Table ports) {
  164   		viewer.setCellEditors(new CellEditor[] { null,
  165   				new TextCellEditor(ports) });
  166   
  167   		ICellModifier cellModifier = new ICellModifier() {
  168   			public Object getValue(Object element, String property) {
  169   				ServerPort sp = (ServerPort) element;
  170   				return sp.getPort() + "";
  171   			}
  172   
  173   			public boolean canModify(Object element, String property) {
  174   				return "port".equals(property);
  175   			}
  176   
  177   			public void modify(Object element, String property, Object value) {
  178   				Item item = (Item) element;
  179   				ServerPort sp = (ServerPort) item.getData();
  180   				GeronimoServerDelegate gs = getGeronimoServer();
  181   				gs.setInstanceProperty(sp.getId(), (String) value);
  182   				changePortNumber(sp.getId(), Integer.parseInt((String) value));
  183   			}
  184   		};
  185   
  186   		viewer.setCellModifier(cellModifier);
  187   	}
  188   
  189   	private void fillTable(Table ports) {
  190   		ServerPort[] serverPorts = getServer().getServerPorts(null);
  191   		if (serverPorts != null) {
  192   			for (int i = 0; i < serverPorts.length; i++) {
  193   				ServerPort port = serverPorts[i];
  194   				TableItem item = new TableItem(ports, SWT.NONE);
  195   				String[] s = new String[] { port.getName(),
  196   						Integer.toString(port.getPort()) };
  197   				item.setText(s);
  198   				item.setImage(Activator.getImage(Activator.IMG_PORT));
  199   				item.setData(port);
  200   			}
  201   		}
  202   	}
  203   
  204   	protected void changePortNumber(String id, int port) {
  205   		TableItem[] items = ports.getItems();
  206   		int size = items.length;
  207   		for (int i = 0; i < size; i++) {
  208   			ServerPort sp = (ServerPort) items[i].getData();
  209   			if (sp.getId().equals(id)) {
  210   				items[i].setData(new ServerPort(id, sp.getName(), port, sp.getProtocol()));
  211   				items[i].setText(1, port + "");
  212   				return;
  213   			}
  214   		}
  215   	}
  216   
  217   	private String getServerName() {
  218   		if (getServer() != null && getServer().getRuntime() != null)
  219   			return getServer().getRuntime().getRuntimeType().getName();
  220   		return null;
  221   	}
  222   
  223   	private IServerWorkingCopy getServer() {
  224   		return (IServerWorkingCopy) getTaskModel().getObject(TaskModel.TASK_SERVER);
  225   	}
  226   
  227   	/*
  228   	 * (non-Javadoc)
  229   	 * 
  230   	 * @see org.eclipse.wst.server.ui.wizard.WizardFragment#isComplete()
  231   	 */
  232   	public boolean isComplete() {
  233   		//TODO
  234   		return true;
  235   	}
  236   
  237   	private GeronimoServerDelegate getGeronimoServer() {
  238   		GeronimoServerDelegate gs = (GeronimoServerDelegate) getServer().getAdapter(GeronimoServerDelegate.class);
  239   		if (gs == null)
  240   			gs = (GeronimoServerDelegate) getServer().loadAdapter(GeronimoServerDelegate.class, null);
  241   		return gs;
  242   	}
  243   
  244   	private GridData createTextGridData() {
  245   		return new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
  246   	}
  247   }

Home » geronimo-eclipse-plugin-2.2-source-release » org.apache.geronimo.st.ui.internal » [javadoc | source]