Package org.dyno.visual.swing.types.editor

Source Code of org.dyno.visual.swing.types.editor.DimensionEditor

/************************************************************************************
* Copyright (c) 2008 William Chen.                                                 *
*                                                                                  *
* 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 *
*                                                                                  *
* Use is subject to the terms of Eclipse Public License v1.0.                      *
*                                                                                  *
* Contributors:                                                                    *
*     William Chen - initial API and implementation.                               *
************************************************************************************/

package org.dyno.visual.swing.types.editor;

import java.awt.Dimension;
import java.util.StringTokenizer;

import org.dyno.visual.swing.plugin.spi.ICellEditorFactory;
import org.dyno.visual.swing.types.endec.DimensionWrapper;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.widgets.Composite;

public class DimensionEditor extends DimensionWrapper implements ICellEditorFactory {
  private static final long serialVersionUID = -4403435758517308113L;

 
  public CellEditor createPropertyEditor(Object bean, Composite parent) {
    CellEditor editor = new TextCellEditor(parent);
    editor.setValidator(new DimensionCellEditorValidator());
    return editor;
  }

 
  public Object decodeValue(Object value) {
    if (value == null)
      return null;
    else if (value.equals("null")) {
      return null;
    } else {
      String sValue = value.toString().trim();
      sValue = sValue.substring(1, sValue.length() - 1);
      StringTokenizer tokenizer = new StringTokenizer(sValue, ",");
      String sWidth = tokenizer.nextToken().trim();
      String sHeight = tokenizer.nextToken().trim();
      int width = 0;
      int height = 0;
      try {
        width = Integer.parseInt(sWidth);
        height = Integer.parseInt(sHeight);
      } catch (NumberFormatException nfe) {
      }
      return new Dimension(width, height);
    }
  }

 
  public Object encodeValue(Object value) {
    if (value == null)
      return null;
    else {
      Dimension dim = (Dimension) value;
      return "(" + dim.width + ", " + dim.height + ")";
    }
  }
}

TOP

Related Classes of org.dyno.visual.swing.types.editor.DimensionEditor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.