examplegui.java

来自「java的swt图形程序」· Java 代码 · 共 2,141 行 · 第 1/5 页

JAVA
2,141 行
字号
    Object[] ints = m_Selection.values().toArray();
    int[] erg = new int[ints.length];

    for (int i = 0; i < erg.length; i++) {
      erg[i] = ((Integer) ints[i]).intValue();
    }
    return erg;
  }

  /**
   * Returns an array of the selected cells as Point[]. The columns are stored
   * in the x fields, rows in y fields. Returns null if in Row Selection Mode.
   * Returns an array with one or none element if not in Multi Selection Mode.
   * 
   * @return int[]
   */
  public Point[] getCellSelection() {
    if (m_RowSelectionMode)
      return null;
    if (!m_MultiSelectMode) {
      if (m_FocusRow < 0 || m_FocusCol < 0)
        return new Point[0];
      Point[] tmp = new Point[1];
      tmp[0] = new Point(m_FocusCol, m_FocusRow);
      return tmp;
    }

    return (Point[]) m_Selection.values().toArray(new Point[1]);
  }

  // ////////////////////////////////////////////////////////////////////////////
  // MODEL
  // ////////////////////////////////////////////////////////////////////////////

  /**
   * Sets the table model. The table model provides data to the table.
   * 
   * @see de.kupzog.ktable.KTableModel for more information.
   * @param model
   */
  public void setModel(KTableModel model) {
    m_Model = model;
    m_FocusCol = -1;
    m_FocusRow = -1;
    clearSelectionWithoutRedraw();
    redraw();
  }

  /**
   * returns the current table model
   * 
   * @return KTableModel
   */
  public KTableModel getModel() {
    return m_Model;
  }

}

/*******************************************************************************
 * Copyright (C) 2004 by Friederich Kupzog Elektronik & Software 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: Friederich Kupzog - initial API and implementation
 * fkmk@kupzog.de www.kupzog.de/fkmk
 ******************************************************************************/

abstract class KTableCellEditor {

  protected KTableModel m_Model;

  protected KTable m_Table;

  protected Rectangle m_Rect;

  protected int m_Row;

  protected int m_Col;

  protected Control m_Control;

  protected String toolTip;

  /**
   * disposes the editor and its components
   */
  public void dispose() {
    if (m_Control != null) {
      m_Control.dispose();
      m_Control = null;
    }
  }

  /**
   * Activates the editor at the given position.
   * 
   * @param row
   * @param col
   * @param rect
   */
  public void open(KTable table, int col, int row, Rectangle rect) {
    m_Table = table;
    m_Model = table.getModel();
    m_Rect = rect;
    m_Row = row;
    m_Col = col;
    if (m_Control == null) {
      m_Control = createControl();
      m_Control.setToolTipText(toolTip);
      m_Control.addFocusListener(new FocusAdapter() {
        public void focusLost(FocusEvent arg0) {
          close(true);
        }
      });
    }
    setBounds(m_Rect);
    GC gc = new GC(m_Table);
    m_Table.drawCell(gc, m_Col, m_Row);
    gc.dispose();
  }

  /**
   * Deactivates the editor.
   * 
   * @param save
   *            If true, the content is saved to the underlying table.
   */
  public void close(boolean save) {
    m_Table.m_CellEditor = null;
    // m_Control.setVisible(false);
    GC gc = new GC(m_Table);
    m_Table.drawCell(gc, m_Col, m_Row);
    gc.dispose();
    this.dispose();
  }

  /**
   * Returns true if the editor has the focus.
   * 
   * @return boolean
   */
  public boolean isFocused() {
    if (m_Control == null)
      return false;
    return m_Control.isFocusControl();
  }

  /**
   * Sets the editor's position and size
   * 
   * @param rect
   */
  public void setBounds(Rectangle rect) {
    if (m_Control != null)
      m_Control.setBounds(rect);
  }

  /*
   * Creates the editor's control. Has to be overwritten by useful editor
   * implementations.
   */
  protected abstract Control createControl();

  protected void onKeyPressed(KeyEvent e) {
    if ((e.character == '\r') && ((e.stateMask & SWT.SHIFT) == 0)) {
      close(true);
    } else if (e.character == SWT.ESC) {
      close(false);
    } else {
      m_Table.scrollToFocus();
    }
  }

  protected void onTraverse(TraverseEvent e) {
    close(true);
    // m_Table.tryToOpenEditorAt(m_Col+1, m_Row);
  }

  /**
   * @param toolTip
   */
  public void setToolTipText(String toolTip) {
    this.toolTip = toolTip;
  }

}

/*******************************************************************************
 * Copyright (C) 2004 by Friederich Kupzog Elektronik & Software 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: Friederich Kupzog - initial API and implementation
 * fkmk@kupzog.de www.kupzog.de/fkmk
 ******************************************************************************/

class KTableCellEditorCombo extends KTableCellEditor {
  private CCombo m_Combo;

  private String m_Items[];

  public void open(KTable table, int row, int col, Rectangle rect) {
    super.open(table, row, col, rect);
    m_Combo.setFocus();
    m_Combo.setText((String) m_Model.getContentAt(m_Col, m_Row));
  }

  public void close(boolean save) {
    if (save)
      m_Model.setContentAt(m_Col, m_Row, m_Combo.getText());
    super.close(save);
    m_Combo = null;
  }

  protected Control createControl() {
    m_Combo = new CCombo(m_Table, SWT.READ_ONLY);
    m_Combo.setBackground(Display.getCurrent().getSystemColor(
        SWT.COLOR_LIST_BACKGROUND));
    if (m_Items != null)
      m_Combo.setItems(m_Items);
    m_Combo.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        try {
          onKeyPressed(e);
        } catch (Exception ex) {
        }
      }
    });
    /*
     * m_Combo.addTraverseListener(new TraverseListener() { public void
     * keyTraversed(TraverseEvent arg0) { onTraverse(arg0); } });
     */
    return m_Combo;
  }

  public void setBounds(Rectangle rect) {
    super.setBounds(new Rectangle(rect.x, rect.y + 1, rect.width,
        rect.height - 2));
  }

  public void setItems(String items[]) {
    m_Items = items;
  }

}

/*******************************************************************************
 * Copyright (C) 2004 by Friederich Kupzog Elektronik & Software 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: Friederich Kupzog - initial API and implementation
 * fkmk@kupzog.de www.kupzog.de/fkmk
 ******************************************************************************/
class KTableCellEditorMultilineText extends KTableCellEditor {
  private Text m_Text;

  public void open(KTable table, int col, int row, Rectangle rect) {
    super.open(table, col, row, rect);
    m_Text.setText(m_Model.getContentAt(m_Col, m_Row).toString());
    m_Text.selectAll();
    m_Text.setVisible(true);
    m_Text.setFocus();
  }

  public void close(boolean save) {
    if (save)
      m_Model.setContentAt(m_Col, m_Row, m_Text.getText());
    m_Text = null;
    super.close(save);
  }

  protected Control createControl() {
    m_Text = new Text(m_Table, SWT.MULTI | SWT.V_SCROLL);
    m_Text.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        try {
          onKeyPressed(e);
        } catch (Exception ex) {
        }
      }
    });
    m_Text.addTraverseListener(new TraverseListener() {
      public void keyTraversed(TraverseEvent arg0) {
        onTraverse(arg0);
      }
    });
    return m_Text;
  }

  /*
   * overridden from superclass
   */
  public void setBounds(Rectangle rect) {
    super.setBounds(new Rectangle(rect.x, rect.y, rect.width, rect.height));
  }

}

/*******************************************************************************
 * Copyright (C) 2004 by Friederich Kupzog Elektronik & Software 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: Friederich Kupzog - initial API and implementation
 * fkmk@kupzog.de www.kupzog.de/fkmk
 ******************************************************************************/
class KTableCellEditorMultilineWrapText extends KTableCellEditor {
  private Text m_Text;

  public void open(KTable table, int col, int row, Rectangle rect) {
    super.open(table, col, row, rect);
    m_Text.setText(m_Model.getContentAt(m_Col, m_Row).toString());
    m_Text.selectAll();
    m_Text.setVisible(true);
    m_Text.setFocus();
  }

  public void close(boolean save) {
    if (save)
      m_Model.setContentAt(m_Col, m_Row, m_Text.getText());
    m_Text = null;
    super.close(save);
  }

  protected Control createControl() {
    m_Text = new Text(m_Table, SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
    m_Text.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        try {
          onKeyPressed(e);
        } catch (Exception ex) {
        }
      }
    });
    m_Text.addTraverseListener(new TraverseListener() {
      public void keyTraversed(TraverseEvent arg0) {
        onTraverse(arg0);
      }
    });
    return m_Text;
  }

  /*
   * overridden from superclass
   */
  public void setBounds(Rectangle rect) {
    super.setBounds(new Rectangle(rect.x, rect.y, rect.width, rect.height));
  }

}

/*******************************************************************************
 * Copyright (C) 2004 by Friederich Kupzog Elektronik & Software 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: Friederich Kupzog - initial API and implementation
 * fkmk@kupzog.de www.kupzog.de/fkmk
 ******************************************************************************/

class KTableCellEditorText extends KTableCellEditor {
  private Text m_Text;

  public void open(KTable table, int col, int row, Rectangle rect) {
    super.open(table, col, row, rect);
    m_Text.setText(m_Model.getContentAt(m_Col, m_Row).toString());
    m_Text.selectAll();
    m_Text.setVisible(true);
    m_Text.setFocus();
  }

  public void close(boolean save) {
    if (save)
      m_Model.setContentAt(m_Col, m_Row, m_Text.getText());
    super.close(save);
    m_Text = null;
    // System.out.println("set to null.");
  }

  protected Control createControl() {
    // System.out.println("Created a new one.");
    m_Text = new Text(m_Table, SWT.NONE);
    m_Text.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        try {
          onKeyPressed(e);
        } catch (Exception ex) {
        }
      }
    });
    m_Text.addTraverseListener(new TraverseListener() {
      public void keyTraversed(TraverseEvent arg0) {
        onTraverse(arg0);
      }
    });
    return m_Text;
  }

  /*
   * overridden from superclass
   */
  public void setBounds(Rectangle rect) {
    super.setBounds(new Rectangle(rect.x, rect.y + (rect.height - 15) / 2
        + 1, rect.width, 15));
  }

}

/*******************************************************************************
 * Copyright (C) 2004 by Friederich Kupzog Elektronik & Software 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: Friederich Kupzog - initial API and implementation
 * fkmk@kupzog.de www.kupzog.de/fkmk
 ******************************************************************************/
class KTableCellRenderer 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?