📄 cell.java
字号:
/**
* Copyright 2005 Victor Ferrer
*
* This file is part of FreeSudoku.
*
* FreeSudoku is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* FreeSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FreeSudoku; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA*
*/
package org.gos.freesudoku.view;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.gos.freesudoku.CONSTS;
/**
* @author gos
*
*/
public class Cell extends JTextArea
{
private static final long serialVersionUID = 1L;
protected static final Insets smallCellSingleLineMargin = new Insets(8,2,2,2);
protected static final Insets smallCellDoubleLineMargin = new Insets(2,2,2,2);
private static final Insets bigCellMargin = new Insets(2,9,2,9);
private KeyEvent keyEvt = null;
private final int i; // Coordenades inicials
private final int j;
protected boolean initPos = false; // Si es va mostrar inicialment
public static Board board = null; // Referencia per comunicarse amb el board
private static Cell currPos = null;
protected boolean isSmall = false;
/**
* @param i
* @param j
*/
public Cell(final int i, final int j)
{
super();
this.i = i;
this.j = j;
initCasella();
}
private void initCasella()
{
setEditable( false);
setForeground( Color.BLUE);
setNormalText();
setMargin( bigCellMargin);
setLineWrap( true);
setWrapStyleWord( false);
this.addMouseListener( new MouseAdapter() {
public void mouseClicked( MouseEvent evt) {
casellaMouseClicked( evt);
}
});
this.addKeyListener( new KeyAdapter(){
public void keyTyped( KeyEvent evt)
{
casellaKeyTyped( evt);
}
});
}
public void casellaKeyTyped(KeyEvent evt)
{
keyEvt = evt;
board.setMessage("");
if( currPos == null || board.disabled ||
( isSmall && getText().length() >= 8 && evt.getKeyChar() >= '1' && evt.getKeyChar() <= '9') ||
(( evt.getKeyChar() < '1' || evt.getKeyChar() > '9') &&
evt.getKeyChar() != ' ') &&
evt.getKeyChar() != KeyEvent.VK_BACK_SPACE)
{
if( getText().length() <= 4) // switch single line
setMargin( smallCellSingleLineMargin);
evt.consume();
return;
}
char charPressed = evt.getKeyChar() == ' ' ? '0' : evt.getKeyChar();
if ( charPressed >= '0' && charPressed <= '9')
{
int val = new Integer( String.valueOf( charPressed)).intValue();
if( isSmall)
{
if( getText().indexOf( charPressed) != -1 || !board.cParent.posKeyTypedSmall( i, j, val)) // if not legal
{
keyEvt.consume();
return;
}
if( getText().length() == 4) // switch double line
setMargin( smallCellDoubleLineMargin);
}
else
{
board.cParent.posKeyTyped( i, j, val);
}
} else
{
if( getText().length() <= 5) // switch single line
setMargin( smallCellSingleLineMargin);
}
}
public void casellaMouseClicked(MouseEvent evt)
{
board.setMessage("");
if( board.disabled) return;
if( currPos != null)
{
if( currPos.getText().length() == 0) // If user types nothing -> back to normal
{
currPos.isSmall = false;
currPos.setNormalText();
}
currPos.setBgColorNormal();
}
if( !initPos)
{
if( evt.getClickCount() == 2)
{
isSmall = !isSmall;
if( isSmall)
{
board.cParent.posKeyTyped( i, j, 0); // Maybe there was a value
setSmallText();
}
else
setNormalText();
}
setBgColorEdit();
currPos = this;
if( evt.isControlDown() && evt.isShiftDown() && evt.isAltDown())
{
board.showAllPosibleVals();
}
else if( evt.isControlDown() && evt.isShiftDown() && (isSmall || getText().length()==0))
{
isSmall = true;
setSmallText();
setText( board.cParent.getPosibleVals( i, j, false));
if( getText().length() > 4) // switch double line
setMargin( smallCellDoubleLineMargin);
}
else if( evt.isControlDown())
board.setMessage( board.cParent.getPosibleVals( i, j, true));
}
else
{
currPos = null;
}
requestFocusInWindow();
}
public void setValue( int val)
{
if( isSmall)
{
if( val == 0)
{
setText("");
if( keyEvt != null) keyEvt.consume();
}
}
else
{
setText( val == 0 ? "" : String.valueOf( val));
}
}
protected void setSmallText()
{
setEditable( true);
getCaret().setVisible( true);
setFont(new Font( CONSTS.FONT_NAME, Font.PLAIN, 10));
setBackground( Color.YELLOW);
setMargin( smallCellSingleLineMargin);
}
public void setNormalText()
{
setEditable( false);
getCaret().setVisible( false);
setFont(new Font( CONSTS.FONT_NAME, Font.BOLD, 18));
setBackground( CONSTS.BLUE_CLEAR);
setMargin( bigCellMargin);
setText(""); // Clean up
}
private void setBgColorEdit()
{
if( isSmall)
setBackground( Color.YELLOW);
else
setBackground( new Color(252, 252, 252));
}
private void setBgColorNormal()
{
if( isSmall)
setBackground( Color.YELLOW);
else
setBackground( CONSTS.BLUE_CLEAR);
}
protected String getPosSmall()
{
if( isSmall) return getText();
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -