📄 simpledp.java
字号:
package norman.baba.algorithms;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import javax.swing.text.DefaultCaret;
import norman.baba.UI.*;
import norman.baba.grids.*;
/**
* <p>Title: BABA</p>
* <p>Description: Bioinformatique Research Project</p>
* <p>Copyright: Copyright Norman Casagrande (c) 2003</p>
* <p>Company: </p>
* @author Norman Casagrande
* @version 1.0
*/
public class SimpleDP
implements CellInteractInterface {
protected String m_defS1 = "";
protected String m_defS2 = "";
/**
* **********************************************************************
* User Interface Stuffs
* **********************************************************************
*/
/**
* ****************** Useful things *********************************
*/
protected Border m_defaultStringBorder = BorderFactory.createCompoundBorder(
BorderFactory.
createEtchedBorder(Color.white, new Color(165, 163, 151)),
BorderFactory.createEmptyBorder(1, 2, 3, 2));
protected Font m_resultAreaFont = new Font("Monospaced", 0, 11);
/**
* ************************* Panels *********************************
*/
/** The main panel. Everything is attached here */
protected JPanel m_mainPane = null;
/**
* The Scroll Area where the grid is painted.
* NOTE: It is a ScrollPane and NOT a JScrollPane because this is way
* faster. Double buffering is controlled by the grid class.
*/
protected ScrollPane m_gridScrollArea = new ScrollPane();
/** The grid class - CENTER */
protected DPTable m_dpTable;
/**
* ********************** NORTH AREA *********************************
*/
protected JTextField m_infoLabel = new JTextField(" Waiting..", 40);
/**
* ********************** SOUTH AREA *********************************
*/
protected JTextArea m_bottomResultArea = new JTextArea(3, 50);
/**
* ********************** EAST AREA *********************************
*/
protected JPanel m_clearPanel = new JPanel();
protected JPanel m_currStatusPanel = new JPanel();
protected JPanel m_stepsButtonPanel = new JPanel();
protected JLabel m_gapOne_title = new JLabel("S1 Gap Penalty Array:");
protected JLabel m_gapTwo_title = new JLabel("S2 Gap Penalty Array:");
// These elements are public
public JTextField m_StringOne = new JTextField(22);
public JTextField m_StringTwo = new JTextField(22);
public JTextField m_gapOne = new JTextField(22);
public JTextField m_gapTwo = new JTextField(22);
////////
public JButton m_btnSetOne = new JButton("Set");
public JButton m_btnSetTwo = new JButton("Set");
public JButton m_btnSetGapOne = new JButton("Set");
public JButton m_btnSetGapTwo = new JButton("Set");
////////
public JLabel m_lDEqual = new JLabel("D(S1,S2) = Min");
public JLabel m_l1Choiche = new JLabel("D(S1-1, S2) + 1");
public JLabel m_l2Choiche = new JLabel("D(S1, S2-1) + 1");
public JLabel m_l3Choiche = new JLabel("D(S1 - 1, S2 - 1) + [1|0]");
////////
public JButton m_btnBeginning = new JButton("|<");
public JButton m_btnNext = new JButton(">");
public JButton m_btnPrev = new JButton("<");
public JButton m_btnEnd = new JButton(">|");
////////
public JButton m_btnClear = new JButton("Clear");
protected static String emptyStringMessage = "[Please Set String]";
protected static String emptyGapMessage = "[Not Set]";
/////////////////////////////////////////////////////////////////////
/**
* This class draws the three lines a the center of the right panel.
* (From D(S1,S2) to the choiches.
*/
protected class DrawablePanel
extends JPanel {
public void paintComponent(Graphics g) {
//super.paintComponent(g);
int startY;
int width = this.getSize().width - 6;
// /
startY = this.getSize().height / 2 - 4;
g.drawLine(4, startY, width, startY - 15);
// --
startY = this.getSize().height / 2;
g.drawLine(4, startY, width, startY);
// \
startY = this.getSize().height / 2 + 4;
g.drawLine(4, startY, width, startY + 15);
}
}
DrawablePanel m_dwPanel = new DrawablePanel();
/**
* **********************************************************************
* Dynamic Programming Stuffs
* **********************************************************************
*/
protected String m_s1 = null;
protected int m_s1_size = 0;
protected String m_s2 = null;
protected int m_s2_size = 0;
protected String[] m_resLine = new String[3];
public static final int STRING_ONE = 0; // VERTICAL string
public static final int STRING_TWO = 1; // HORIZONTAL string
protected boolean m_tableReady = false;
protected int m_currentStep = 0;
protected int m_gapPenaltyOne[];
protected int m_gapPenaltyTwo[];
protected static final int GAP_ONE = 0;
protected static final int GAP_TWO = 1;
/** In which state is the current calculus: phase calculating the grid */
protected static final int PHASE_CALC_GRID = 0;
/** In which state is the current calculus: phase backtracking the pointers */
protected static final int PHASE_BACKTRACK = 1;
/** The current calculating phase. At the beginning is calculating the grid */
public int m_currentPhase = PHASE_CALC_GRID;
// Linked list of the backtrack cells
protected LinkedList m_backTrackList = new LinkedList();
protected CellElement m_backtrackLastSel = null;
protected int m_backtrackingPolicy = CellElement.
POINTER_POLICY_COUNTERCLOCKWISE;
/**
* **********************************************************************
* The class
* **********************************************************************
*/
/** Constructor */
public SimpleDP(JPanel contentPane,
String defaultString1, String defaultString2) {
m_mainPane = contentPane;
m_mainPane.setLayout(new BorderLayout());
m_defS1 = defaultString1;
m_defS2 = defaultString2;
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
public SimpleDP(JPanel contentPane) {
this(contentPane, "", "");
}
public String getAlgorithmName() {
return "Simple Dynamic Programming";
}
//Component initialization
protected void jbInit() throws Exception {
/**
* ************************* Panels *********************************
*/
/** The top panel - NORTH */
JPanel topPanel = new JPanel();
/** The bottom panel - SOUTH */
JPanel bottomPanel = new JPanel();
/** The right panel - EAST */
JPanel rightPanel = new JPanel();
/**
* ********************** CENTER AREA *********************************
*/
this.setCenter();
/**
* ********************** NORTH AREA ********************************
*/
this.setNorth(topPanel);
/**
* ********************** SOUTH AREA ********************************
*/
this.setSouth(bottomPanel);
/**
* ********************** EAST AREA *********************************
*/
this.setEast(rightPanel);
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// Now adds everything to the main panel
m_mainPane.add(topPanel, BorderLayout.NORTH);
m_mainPane.add(m_gridScrollArea, BorderLayout.CENTER);
m_mainPane.add(rightPanel, BorderLayout.EAST);
m_mainPane.add(bottomPanel, BorderLayout.SOUTH);
}
/**
* ********************** CENTER AREA *********************************
*/
protected void setCenter() {
m_dpTable = new DPTable(10, 6);
m_dpTable.setCellListener(this);
m_gridScrollArea.add(m_dpTable);
// This is in the case of JScrollArea instead of ScrollArea
//m_gridScrollArea.setDoubleBuffered(true);
//m_gridScrollArea.getViewport().setView(m_dpTable);
}
/**
* ********************** NORTH AREA *********************************
*/
protected void setNorth(JPanel topPanel) {
// JComboBox aliasingComboBox = new JComboBox(aliasingData);
int i;
String[] aliasingData = {
"On", "Off"};
Choice aliasingComboBox = new Choice();
for (i = 0; i < aliasingData.length; ++i) {
aliasingComboBox.add(aliasingData[i]);
}
// JComboBox zoomComboBox = new JComboBox(zoomData);
String[] zoomData = {
"-2", "-1", "Normal", "+1", "+2", "+3"};
Choice zoomComboBox = new Choice();
for (i = 0; i < zoomData.length; ++i) {
zoomComboBox.add(zoomData[i]);
}
/* Dimension prevDim = zoomComboBox.getPreferredSize();
prevDim.width += 2;
zoomComboBox.setPreferredSize(prevDim);
*/
zoomComboBox.select(2); // Selecting Normal Zoom
m_infoLabel.setBorder(m_defaultStringBorder);
m_infoLabel.setEditable(false);
topPanel.add(new JLabel("AntiAliasing:"));
topPanel.add(aliasingComboBox);
topPanel.add(new JLabel("Zoom:"));
topPanel.add(zoomComboBox);
topPanel.add(new JLabel("Info:"));
topPanel.add(m_infoLabel);
// Listeners
aliasingComboBox.addItemListener(new AliasingComboListener());
zoomComboBox.addItemListener(new ZoomComboListener());
}
/**
* ********************** SOUTH AREA *********************************
*/
protected void setSouth(JPanel bottomPanel) {
JTextArea bottomLabelArea = new JTextArea("S1:\n\nS2:", 3, 4);
m_bottomResultArea.setFont(m_resultAreaFont);
bottomLabelArea.setFont(m_resultAreaFont);
m_bottomResultArea.setBorder(m_defaultStringBorder);
bottomLabelArea.setBorder(m_defaultStringBorder);
m_bottomResultArea.setEditable(false);
bottomLabelArea.setEditable(false);
bottomLabelArea.setBackground(UIManager.getColor("Label.background"));
// Redefining height to be 3 rows: the default was not correct.
FontMetrics fm = m_mainPane.getFontMetrics(m_resultAreaFont);
int areasHeight = fm.getHeight() * 3 + fm.getDescent() * 3 - 2;
m_bottomResultArea.setPreferredSize(new Dimension(400, areasHeight));
bottomLabelArea.setPreferredSize(new Dimension(bottomLabelArea.
getPreferredSize().width,
areasHeight));
bottomPanel.add(bottomLabelArea);
bottomPanel.add(m_bottomResultArea);
}
/**
* ********************** EAST AREA *********************************
*/
protected void setEast(JPanel rightPanel) {
/**
* This panel is the only object in "rightPanel". I did this to
* add some insets on every border.
*/
JPanel rightInsPanel = new JPanel();
rightInsPanel.setLayout(new GridBagLayout());
/** In the "rightInsPanel" panel there is: */
m_currStatusPanel.setLayout(new GridBagLayout());
JLabel stringOne_title = new JLabel("String S1:");
JLabel stringTwo_title = new JLabel("String S2:");
///////////////////////////////////////////////////////
m_StringOne.setText(emptyStringMessage);
m_StringTwo.setText(emptyStringMessage);
m_gapOne.setText(emptyGapMessage);
m_gapTwo.setText(emptyGapMessage);
m_StringOne.setEditable(false);
m_StringTwo.setEditable(false);
m_gapOne.setEditable(false);
m_gapTwo.setEditable(false);
m_btnSetGapOne.setEnabled(false);
m_btnSetGapTwo.setEnabled(false);
m_StringOne.setBorder(m_defaultStringBorder);
m_StringTwo.setBorder(m_defaultStringBorder);
m_gapOne.setBorder(m_defaultStringBorder);
m_gapTwo.setBorder(m_defaultStringBorder);
///////////////////////////////////////////////////////
m_l1Choiche.setOpaque(true);
m_l2Choiche.setOpaque(true);
m_l3Choiche.setOpaque(true);
///////////////////////////////////////////////////////
m_btnBeginning.setEnabled(false);
m_btnBeginning.setMargin(new Insets(2, 8, 2, 8));
m_btnNext.setEnabled(false);
m_btnPrev.setEnabled(false);
m_btnPrev.setMargin(new Insets(2, 8, 2, 8));
m_btnEnd.setEnabled(false);
m_btnEnd.setMargin(new Insets(2, 8, 2, 8));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -