All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scudata.ide.spl.chart.TableParamEdit Maven / Gradle / Ivy

Go to download

SPL(Structured Process Language) A programming language specially for structured data computing.

There is a newer version: 20241126
Show newest version
package com.scudata.ide.spl.chart;

import java.awt.event.*;

import javax.swing.*;
import javax.swing.table.*;

import java.util.*;
import java.awt.*;

import com.scudata.cellset.datamodel.PgmNormalCell;
import com.scudata.chart.*;
import com.scudata.chart.edit.*;
import com.scudata.common.StringUtils;
import com.scudata.dm.*;
import com.scudata.ide.common.*;
import com.scudata.ide.common.dialog.DialogInputText;
import com.scudata.ide.common.swing.*;
import com.scudata.ide.spl.GVSpl;
import com.scudata.ide.spl.chart.box.*;
import com.scudata.ide.spl.resources.*;
import com.scudata.util.*;

/**
 * ?????༭????
 */
public class TableParamEdit extends JTableEx {
	private static final long serialVersionUID = 1L;
	
	private String FOLDERCOL = " ";  //"ͼ????";
	public String NAMECOL = ChartMessage.get().getMessage( "label.propname" );  //"????????";
	public String VALUECOL = ChartMessage.get().getMessage( "label.propvalue" );  //"????ֵ";
	public String EXPCOL = ChartMessage.get().getMessage( "label.propexp" );  //"????ֵ????ʽ";
	public String AXISCOL = ChartMessage.get().getMessage( "label.axis" );  //"??????";
	private String EDITSTYLECOL = "editstyle";
	private String OBJCOL = "objcol"; //ParamInfo?????????null???????????
	private String ARROWCOL = new String(new char[]{0});
	
	public static int iFOLDERCOL = 0;
	public static int iNAMECOL = 1;
	public static int iVALUECOL = 2;
	public static int iEXPCOL = 3;
	public static int iAXISCOL = 4;
	public static int iEDITSTYLECOL = 5;
	public static int iOBJCOL = 6;
	public static int iARROWCOL = 7;
	
	private int COLCOUNT = 8;
	

	private HashMap> hiddenMap = new HashMap>(); //?????е????? ????ArrayList ÿ??????Object[]
	public TableInputSeries seriesTable;
	
	boolean isAutoHide = false;
	
	/**
	 * ????һ???????༭??
	 * @param owner ??????
	 * @param list ??????Ϣ?б?
	 */
	public TableParamEdit( Dialog owner, ParamInfoList list ) {
		String[] colNames = new String[] { FOLDERCOL, NAMECOL, VALUECOL, EXPCOL, AXISCOL, EDITSTYLECOL, OBJCOL, ARROWCOL };
		data.setColumnIdentifiers( colNames );

		//????????
		ArrayList root = list.getRootParams();
		for( int i = 0; i < root.size(); i++ ) {
			ParamInfo pi = ( ParamInfo ) root.get( i );
			Object row[] = new Object[COLCOUNT];
			row[iFOLDERCOL] = null;
			row[iNAMECOL] = pi.getTitle();
			Object value = pi.getValue();
			if( value instanceof String ) {
				if( value.toString().startsWith( "=" ) ) {
					row[iEXPCOL] = value.toString().substring( 1 );
					row[iVALUECOL] = pi.getDefValue();
				}
				else row[iVALUECOL] = value;
			}
			else {
				if( value instanceof ArrayList ) {
					row[iVALUECOL] = arrayList2Series( ( ArrayList ) value );
					pi.setValue( row[iVALUECOL] );
				}
				else row[iVALUECOL] = value;
			}
			row[iAXISCOL] = pi.getAxis();
			row[iEDITSTYLECOL] = new Integer( pi.getInputType() );
			row[iOBJCOL] = pi;
			data.addRow( row );
		}
		java.util.List groups = list.getGroupNames();
		if( groups != null ) {
			for ( int i = 0; i < groups.size(); i++ ) {
				String groupName = (String) groups.get( i );
				Object[] grow = new Object[COLCOUNT];
				if( i == groups.size() - 1 ) grow[iFOLDERCOL] = new Byte( GC.TYPE_LASTMINUS );
				else grow[iFOLDERCOL] = new Byte( GC.TYPE_MINUS );
				grow[iNAMECOL] = groupName;
				grow[iEDITSTYLECOL] = new Integer( Consts.INPUT_NORMAL );
				data.addRow( grow );
				ArrayList grows = new ArrayList();
				ArrayList glist = list.getParams( groupName );
				for( int j = 0; j < glist.size(); j++ ) {
					ParamInfo pi = ( ParamInfo ) glist.get( j );
					Object row[] = new Object[COLCOUNT];
					if( j == glist.size() - 1 ) row[iFOLDERCOL] = new Byte( GC.TYPE_LASTNODE );
					else row[iFOLDERCOL] = new Byte( GC.TYPE_NODE );
					row[iNAMECOL] = pi.getTitle();
					Object value = pi.getValue();
					if( value instanceof String ) {
						if( value.toString().startsWith( "=" ) ) {
							row[iEXPCOL] = value.toString().substring( 1 );
							row[iVALUECOL] = pi.getDefValue();
						}
						else row[iVALUECOL] = value;
					}
					else {
						if( value instanceof ArrayList ) {
							row[iVALUECOL] = arrayList2Series( ( ArrayList ) value );
							pi.setValue( row[iVALUECOL] );
						}
						else row[iVALUECOL] = value;
					}
					row[iAXISCOL] = pi.getAxis();
					row[iEDITSTYLECOL] = new Integer( pi.getInputType() );
					row[iOBJCOL] = pi;
					data.addRow( row );
					grows.add( row );
				}
				hiddenMap.put( groupName, grows );
			}
		}

		this.setRowHeight( 25 );
		DefaultParamTableRender render = new DefaultParamTableRender();
		TableColumn tc;
		tc = getColumn( iFOLDERCOL );
		tc.setMaxWidth( 20 );
		tc.setMinWidth( 20 );
		tc.setCellEditor( new ImageEditor() );
		tc.setCellRenderer( new ImageRenderer() );

		tc = getColumn( iNAMECOL );
		tc.setCellRenderer( render );
		tc.setPreferredWidth( 100 );
		tc.setMaxWidth( 200 );

		tc = getColumn( iVALUECOL );
		tc.setCellEditor( new EachRowEditor( this, iEDITSTYLECOL, owner ) );
		tc.setCellRenderer( new EachRowRenderer( iEDITSTYLECOL ) );

		tc = getColumn( iEXPCOL );
		tc.setCellEditor( new JTextAreaEditor( this ) );
		tc.setCellRenderer( render );

		tc = getColumn( iAXISCOL );
		tc.setCellEditor( new JTextAreaEditor( this ) );
		tc.setCellRenderer( render );

		tc = getColumn( iARROWCOL );
		tc.setMaxWidth( 25 );
		tc.setMinWidth( 25 );
		tc.setCellEditor( new SeriesEditor( owner ) );
		tc.setCellRenderer( new SeriesEditRender() );

		setColumnVisible( EDITSTYLECOL, false );
		setColumnVisible( OBJCOL, false );

		setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		setColumnSelectionAllowed( true ); //?п?ѡ
		setRowSelectionAllowed( true ); //?п?ѡ
		this.addFocusListener( new FocusListener() {
			public void focusGained(FocusEvent e) {
				if(seriesTable!=null){
					seriesTable.updateParams();
				}
			}
			public void focusLost(FocusEvent e) {
			}
		});
	}
	
	/**
	 * ?Զ?????ͼ??ʱ?????ض?????
	 */
	public void autoHide(){
		isAutoHide = true;
		setColumnVisible( EXPCOL, false );
		setColumnVisible( AXISCOL, false );
		setColumnVisible( ARROWCOL, false );
	}
	
	/**
	 * ʵ????????¼?
	 * @param e ????¼?
	 */
	public void mouseClicked( MouseEvent e ) {
		super.mouseClicked( e );
		if ( e.getButton() == MouseEvent.BUTTON1 ) {
			int row = getSelectedRow();
			int col = getSelectedColumn();
			if ( col != iFOLDERCOL ) {  //???????????ʱ????????չ????????
				return;
			}
			if ( data.getValueAt( row, col ) == null || ! ( data.getValueAt( row, col ) instanceof Byte ) ) {
				return;
			}
			byte oldType = ( ( Byte ) data.getValueAt( row, col ) ).byteValue();
			byte newType = oldType;
			ArrayList list = new ArrayList();
			Object rowData[];
			String key = ( String ) data.getValueAt( row, iNAMECOL );
			acceptText();
			switch ( oldType ) {
				case GC.TYPE_MINUS:
					newType = GC.TYPE_PLUS;
				case GC.TYPE_LASTMINUS:
					newType = GC.TYPE_LASTPLUS;
					while ( row + 1 < data.getRowCount() && data.getValueAt( row + 1, iOBJCOL ) != null ) { //?ҵ???β????һ????????Ϊֹ
						rowData = new Object[data.getColumnCount()];
						for ( int c = 0; c < data.getColumnCount(); c++ ) {
							rowData[c] = data.getValueAt( row + 1, c );
						}
						list.add( rowData );
						data.removeRow( row + 1 );
					}
					hiddenMap.put( key, list );
					break;
				case GC.TYPE_PLUS:
					newType = GC.TYPE_MINUS;
				case GC.TYPE_LASTPLUS:
					newType = GC.TYPE_LASTMINUS;
					expand( key, row + 1 );
					break;
			}
			data.setValueAt( new Byte( newType ), row, col );
			acceptText();
		}
	}

	/**
	 * չ?????в???
	 */
	public void expandAll(){
		int rowCount = data.getRowCount();
		for(int r = rowCount - 1;r>=0; r--){
			Object obj = data.getValueAt( r, iFOLDERCOL );
			if(obj==null){
				continue;
			}
			byte oldType = ( ( Byte ) obj ).byteValue();
			if(oldType!=GC.TYPE_LASTPLUS) continue;
			String key = ( String ) data.getValueAt( r, iNAMECOL );
			expand( key, r + 1 );
			data.setValueAt( new Byte( GC.TYPE_LASTMINUS ), r, iFOLDERCOL );
		}
		acceptText();
	}
	
	/**
	 * ???????
	 * @param key ?????ķ????ֵ 
	 * @param row ?к?
	 */
	private void collapse(String key, int row){
		ArrayList list = new ArrayList();
		Object rowData[];
		while ( row + 1 < data.getRowCount() && data.getValueAt( row + 1, iOBJCOL ) != null ) { //?ҵ???β????һ????????Ϊֹ
			rowData = new Object[data.getColumnCount()];
			for ( int c = 0; c < data.getColumnCount(); c++ ) {
				rowData[c] = data.getValueAt( row + 1, c );
			}
			list.add( rowData );
			data.removeRow( row + 1 );
		}
		hiddenMap.put( key, list );
	}
	
	/**
	 * ???????в???
	 */
	public void collapseAll(){
		int rowCount = data.getRowCount();
		for(int r = rowCount - 1;r>=0; r--){
			Object obj = data.getValueAt( r, iFOLDERCOL );
			if(obj==null){
				continue;
			}
			byte oldType = ( ( Byte ) obj ).byteValue();
			if(oldType!=GC.TYPE_MINUS && oldType!=GC.TYPE_LASTMINUS) continue;
			String key = ( String ) data.getValueAt( r, iNAMECOL );
			collapse( key, r );
			data.setValueAt( new Byte( GC.TYPE_LASTPLUS ), r, iFOLDERCOL );
		}
		acceptText();
	}
	
	/**
	 * ???ظ????Ƿ??????༭
	 * @param row ??
	 * @param column ??
	 */
	public boolean isCellEditable( int row, int column ) {
		ParamInfo info = ( ParamInfo ) data.getValueAt( row, iOBJCOL );
		if( info == null ) {   //?????в??ܱ༭
			return false;
		}
		
		if( !isAutoHide ){
			if( column == iAXISCOL && !info.isAxisEnable() ) return false;
		}
		
		return column != iNAMECOL;
	}

	/**
	 * ij??????ֵ???޸ĺ󴥷?
	 * @param aValue ????ֵ
	 * @param row ??
	 * @param column ??
	 * 
	 */
	public void setValueAt( Object aValue, int row, int column ) {
		Object oldValue = getValueAt( row, column );
		if( oldValue == null ) oldValue = "";
		if( column == iEXPCOL ) {
			aValue = aValue.toString().trim();
			if(!StringUtils.isValidExpression((String)aValue)){
				JOptionPane.showMessageDialog(GV.appFrame, ChartMessage.get().getMessage( "TableParamEdit.invalidexp",aValue ));
				return;
			}
		}
		if ( Variant.isEquals( aValue, oldValue ) ) {
			return;
		}
		super.setValueAt( aValue, row, column );
		ParamInfo info = ( ParamInfo ) data.getValueAt( row, iOBJCOL );
		if( info != null ) {
			if(isAutoHide){//?Զ?????????к?ֻ??ֵ?п??Ա༭
				info.setValue( aValue );	
				return;
			}
			if( column == iAXISCOL ) {
				info.setAxis( ( String ) aValue );
				return;
			}
			if( column == iVALUECOL ) {   //????ֵ??Ҫ?ڱ???ʽ????ʾ?????????뷽ʽѡ???ֵ
				super.setValueAt( toExpString( info, aValue ), row, iEXPCOL );
				super.setValueAt( "", row, iAXISCOL );   //???????
			}
			else if( column == iEXPCOL ) {  //???????ʽ??Ҫ???ܽ?????ֵ??ʾ??ֵ?ж?Ӧ????ʾֵ
				super.setValueAt( toValueObject( info, aValue.toString() ), row, iVALUECOL );
				if( aValue.toString().trim().length() == iFOLDERCOL ) {
					aValue = info.getDefValue();
					super.setValueAt( "", row, iAXISCOL );   //???????
				}
				else aValue = "=" + aValue.toString();
			}
			else return;
			info.setValue( aValue );
		}
	}

	private void expand( String groupName, int row ) {
		ArrayList list = hiddenMap.get( groupName );
		for ( int i = 0; i < list.size(); i++ ) {
			data.insertRow( row + i,list.get( i ) );
		}
	}

	/**
	 * ???ܵ?ǰ?ı༭ֵ
	 */
	public void acceptText() {
		if ( this.isEditing() ) {
			this.getCellEditor().stopCellEditing();
		}
	}

	/**
	 * ??????value???ݲ?????Ϣ??????תΪ????ʽ??
	 * @param info ??????Ϣ
	 * @param value ????ֵ
	 * @return ????ʽ??
	 */
	public static String toExpString( ParamInfo info, Object value ) {
		int inputType = info.getInputType();
		switch( inputType ) {
			case Consts.INPUT_COLOR:
			case Consts.INPUT_LINESTYLE:
			case Consts.INPUT_TEXTURE:
			case Consts.INPUT_POINTSTYLE:
			case Consts.INPUT_FONTSTYLE:
			case Consts.INPUT_COLUMNSTYLE:
			case Consts.INPUT_DROPDOWN:
			case Consts.INPUT_CHARTCOLOR:
			case Consts.INPUT_ARROW:
			case Consts.INPUT_TICKS:
			case Consts.INPUT_UNIT:
			case Consts.INPUT_COORDINATES:
			case Consts.INPUT_AXISLOCATION:
			case Consts.INPUT_FONTSIZE:
			case Consts.INPUT_HALIGN:
			case Consts.INPUT_VALIGN:
			case Consts.INPUT_LEGENDICON:
			case Consts.INPUT_CUSTOMDROPDOWN:
			case Consts.INPUT_URLTARGET:
				return value == null ? "" : value.toString();
			default:
				return "";
		}
	}

	/**
	 * ??????ʽexp???ղ?????Ϣinfo??????ת??Ϊ??Ӧ?IJ???ֵ
	 * @param info ??????Ϣ
	 * @param exp ????ʽ
	 * @return ????ֵ
	 */
	public static Object toValueObject( ParamInfo info, String exp ) {
		int inputType = info.getInputType();
		switch( inputType ) {
			case Consts.INPUT_NORMAL:
				return Variant.parse(exp, true);
			case Consts.INPUT_EXP:
			case Consts.INPUT_DATE:
			case Consts.INPUT_FILE:
				return "";
			case Consts.INPUT_CHARTCOLOR:
				Object value1 = PgmNormalCell.parseConstValue( exp );
				if( TableInputSeries.isChartColor( value1 ) ) return ChartColor.getInstance( ( Sequence ) value1 );
				return info.getDefValue();
			default:
				Object value = PgmNormalCell.parseConstValue( exp );
				if( isRightType( inputType, value ) ) return value;
				return info.getDefValue();
		}
	}

	/**
	 * ???ز???ֵvalue?Ƿ????????????typeһ??
	 * @param type ???Ͷ???
	 * @param value ????ֵ
	 * @return һ?µĻ?????true?????򷵻?false
	 */
	public static boolean isRightType( int type, Object value ) {
		switch( type ) {
			case Consts.INPUT_ANGLE:
			case Consts.INPUT_ARROW:
			case Consts.INPUT_AXISLOCATION:
			case Consts.INPUT_COLOR:
			case Consts.INPUT_COLUMNSTYLE:
			case Consts.INPUT_COORDINATES:
			case Consts.INPUT_FONTSIZE:
			case Consts.INPUT_FONTSTYLE:
			case Consts.INPUT_HALIGN:
			case Consts.INPUT_VALIGN:
			case Consts.INPUT_INTEGER:
			case Consts.INPUT_LEGENDICON:
			case Consts.INPUT_LINESTYLE:
			case Consts.INPUT_POINTSTYLE:
			case Consts.INPUT_TEXTURE:
			case Consts.INPUT_TICKS:
			case Consts.INPUT_UNIT:
				return value instanceof Integer;
			case Consts.INPUT_DOUBLE:
				return value instanceof Double;
			case Consts.INPUT_FONT:
			case Consts.INPUT_URLTARGET:
			case Consts.INPUT_NORMAL:
				return value instanceof String;
			case Consts.INPUT_CHECKBOX:
				return value instanceof Boolean;
			case Consts.INPUT_CHARTCOLOR:
				return TableInputSeries.isChartColor( value );
		}
		return false;
	}

	/**
	 * ???????б༭??
	 * @param table ???б༭??
	 */
	public void setSeriesTable( TableInputSeries table ) {
		this.seriesTable = table;
	}

	private Sequence arrayList2Series( ArrayList list ) {
		Sequence series = new Sequence();
		for( int i = 0; i < list.size(); i++ ) {
			Object o = list.get( i );
			if( o instanceof ArrayList) {
				series.add( arrayList2Series( (ArrayList)o ) );
			}
			else series.add( o );
		}
		return series;
	}
	
	/**
	 * ʵ?????˫??ʱ??
	 */
	public void doubleClicked(int xpos, int ypos, int row, int col,
			MouseEvent e) {
		if(!isCellEditable(row, col)){
			return;
		}
		if (row > -1 && col==iEXPCOL) {
			DialogInputText dit = new DialogInputText(GVSpl.appFrame, true);
			 String exp = (String) data.getValueAt(row, col);
			dit.setText(exp);
			dit.setVisible(true);
			if (dit.getOption() == JOptionPane.OK_OPTION) {
				 acceptText();
				 setValueAt(dit.getText(), row, col);
				 acceptText();
			}
		}
	}

}