001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.gui.actions;
025    
026    import javax.swing.JComponent;
027    import javax.swing.JOptionPane;
028    
029    import org.ujmp.core.Matrix;
030    import org.ujmp.core.calculation.Calculation.Ret;
031    import org.ujmp.core.interfaces.GUIObject;
032    import org.ujmp.gui.MatrixGUIObject;
033    import org.ujmp.gui.util.GUIUtil;
034    
035    public abstract class AbstractMatrixAction extends ObjectAction {
036            private static final long serialVersionUID = 7005218564058853146L;
037    
038            public static final int ROW = Matrix.ROW;
039    
040            public static final int COLUMN = Matrix.COLUMN;
041    
042            public static final int ALL = Matrix.ALL;
043    
044            private GUIObject variable = null;
045    
046            public AbstractMatrixAction(JComponent c, MatrixGUIObject matrix,
047                            GUIObject v) {
048                    super(c, matrix);
049                    this.variable = v;
050            }
051    
052            public Ret getRet() {
053                    return (Ret) GUIUtil.getObject("Select return method", Ret.ORIG,
054                                    Ret.NEW, Ret.LINK);
055            }
056    
057            public Ret getOrigOrNew() {
058                    return (Ret) GUIUtil.getObject("Select return method", Ret.ORIG,
059                                    Ret.NEW);
060            }
061    
062            public Ret getNewOrLink() {
063                    return (Ret) GUIUtil.getObject("Select return method", Ret.NEW,
064                                    Ret.LINK);
065            }
066    
067            public long[] getSize() {
068                    return GUIUtil
069                                    .getSize("Enter the size of the matrix (e.g. '5,4' -> 5 rows, 4 columns)");
070            }
071    
072            public int getDimension() {
073                    int dimension = -1;
074    
075                    if (dimension == -1) {
076                            if (getMatrixObject().getColumnCount() == 1) {
077                                    dimension = ROW;
078                            } else if (getMatrixObject().getRowCount() == 1) {
079                                    dimension = COLUMN;
080                            }
081                    }
082                    if (dimension == -1) {
083                            int i = JOptionPane.showOptionDialog(getComponent(), "Dimension",
084                                            "Select Dimension", JOptionPane.OK_OPTION,
085                                            JOptionPane.QUESTION_MESSAGE, null, new String[] { "Row",
086                                                            "Column", "All" }, "Row");
087                            if (i == 2) {
088                                    dimension = ALL;
089                            } else {
090                                    dimension = i;
091                            }
092                    }
093                    return dimension;
094            }
095    
096            public boolean getIgnoreMissing() {
097                    if (!getMatrixObject().getMatrix().containsMissingValues()) {
098                            return true;
099                    } else {
100                            return GUIUtil.getBoolean("Ignore missing values");
101                    }
102            }
103    
104            public Matrix getMatrix() {
105                    return getMatrixObject().getMatrix();
106            }
107    
108            public MatrixGUIObject getMatrixObject() {
109                    MatrixGUIObject m = (MatrixGUIObject) getGUIObject();
110                    if (m != null) {
111                            int startRow = m.getRowSelectionModel().getMinSelectionIndex();
112                            int endRow = m.getRowSelectionModel().getMaxSelectionIndex();
113                            int startColumn = m.getColumnSelectionModel()
114                                            .getMinSelectionIndex();
115                            int endColumn = m.getColumnSelectionModel().getMaxSelectionIndex();
116                            if (startRow != -1 && startColumn != -1
117                                            && (startRow != endRow || startColumn != endColumn)) {
118                                    m = (MatrixGUIObject) m.getMatrix().subMatrix(Ret.LINK,
119                                                    startRow, startColumn, endRow, endColumn)
120                                                    .getGUIObject();
121                            }
122                            return m;
123                    } else {
124                            // int min = variable.getRowSelectionModel().getMinSelectionIndex();
125                            // int max = variable.getRowSelectionModel().getMaxSelectionIndex();
126                            // Matrix all = new MatrixListToMatrixWrapper(variable);
127                            //
128                            // if (min >= 0 && max >= 0) {
129                            // Matrix selection = all.subMatrix(Ret.LINK, min, 0, max,
130                            // all.getColumnCount() - 1);
131                            // return (MatrixGUIObject) selection.getGUIObject();
132                            // } else {
133                            // return (MatrixGUIObject) all.getGUIObject();
134                            // }
135                    }
136                    return null;
137            }
138    
139            public void setMatrix(MatrixGUIObject m) {
140                    setGUIObject(m);
141            }
142    
143            public GUIObject getVariable() {
144                    return variable;
145            }
146    
147    }