📄 mapeditorviews.java
字号:
public void remove() {
Object[] a = continentsList.getSelectedValues();
if (a.length!=0) {
String continentsString="";
for (int c=0;c<a.length;c++) {
continentsString = continentsString + "\n" + a[c];
}
int result = JOptionPane.showConfirmDialog(MapEditorViews.this, "Are you sure you want to remove:"+
continentsString
, "Remove?", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
Vector continents = new Vector( Arrays.asList( map.getContinents() ) );
List removeList = Arrays.asList(a);
Vector removeCountreis = new Vector();
for (int c=0;c<a.length;c++) {
removeCountreis.addAll( ((Continent)a[c]).getTerritoriesContained() );
}
removeCountries( removeCountreis.toArray() );
continents.removeAll(removeList);
map.setContinents( (Continent[])continents.toArray( new Continent[continents.size()] ) );
fireContentsChanged(this,0,getSize()-1);
continentsList.clearSelection();
Vector missions = map.getMissions();
for (int c=0;c<missions.size();c++) {
Mission mission = (Mission)missions.elementAt(c);
if (removeList.contains( mission.getContinent1() )) {
mission.setContinent1(null);
}
if (removeList.contains( mission.getContinent2() )) {
mission.setContinent2(null);
}
if (removeList.contains( mission.getContinent3() )) {
mission.setContinent3(null);
}
}
}
}
}
public void add() {
Object[] message = new Object[4];
message[0] = new JLabel("Names: (each line will be made into a new continent)");
message[1] = new OptionPaneTextArea( "" );
message[2] = new JLabel("Initial army value:");
message[3] = new JSpinner( new SpinnerNumberModel( 5,0,100,1) );
int result = openOptionDialog(message,"New continents");
if (result == JOptionPane.OK_OPTION ) {
int armies = ((Integer)((JSpinner)message[3]).getValue()).intValue();
String[] names = ((OptionPaneTextArea)message[1]).getLines();
Continent[] oldContinents = map.getContinents();
Vector newContinents = new Vector( Arrays.asList(oldContinents) );
for (int c=0;c<names.length;c++) {
Continent continent = new Continent(names[c].replace(' ','_'),names[c],armies, RiskGame.getRandomColor() );
newContinents.add(continent);
}
map.setContinents( (Continent[])newContinents.toArray( new Continent[newContinents.size()] ) );
fireContentsChanged(this,oldContinents.length, oldContinents.length+names.length-1 );
editPanel.repaint();
}
}
}
class CardsListModel extends AbstractListModel implements ViewTab {
public void changed() {
super.fireContentsChanged(this, 0, map.getNoCards()-1);
cardsList.clearSelection();
}
public Object getElementAt(int index) {
if (map==null) { return null; }
return map.getCards().elementAt(index);
}
public int getSize() {
if (map==null) { return 0; }
return map.getNoCards();
}
public void edit() {
int sel = cardsList.getSelectedIndex();
if (sel != -1) {
Card card = (Card)getElementAt(sel);
Country country = map.getCountryInt(sel+1);
Object[] message = new Object[2];
message[0] = new JLabel("Type:");
if (country == null || ( sel > 0 && ((Card)getElementAt(sel-1)).getName().equals(Card.WILDCARD) ) ) {
message[1] = new JComboBox( new String[] { Card.WILDCARD } );
}
else if (
( getSize()==(sel+1) || ((Card)getElementAt(sel+1)).getName().equals(Card.WILDCARD) ) &&
( sel == 0 || !((Card)getElementAt(sel-1)).getName().equals(Card.WILDCARD) )
) {
message[1] = new JComboBox( cardTypes );
}
else {
message[1] = new JComboBox( new String[] { Card.CAVALRY,Card.INFANTRY,Card.CANNON } );
}
//message[2] = new JLabel("Country:");
//message[3] = new JComboBox( map.getCountries() );
((JComboBox)message[1]).setSelectedItem( card.getName() );
//((JComboBox)message[3]).setSelectedItem( card.getCountry() );
int result = openOptionDialog(message,"Edit card");
if (result == JOptionPane.OK_OPTION ) {
String newtype = (String)((JComboBox)message[1]).getSelectedItem();
card.setName(newtype);
if (Card.WILDCARD.equals(newtype)) {
card.setCountry(null);
}
else {
//card.setCountry( (Country)((JComboBox)message[3]).getSelectedItem() );
card.setCountry( country );
}
fireContentsChanged(this,sel, sel );
}
}
}
public void remove() {
Object[] a = cardsList.getSelectedValues();
if (a.length!=0) {
String cardsString="";
for (int c=0;c<a.length;c++) {
cardsString = cardsString + "\n" + ((Card)a[c]).getName();
}
int result = JOptionPane.showConfirmDialog(MapEditorViews.this, "Are you sure you want to remove:"+
cardsString
, "Remove?", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
Vector cards = map.getCards();
cards.removeAll( Arrays.asList(a) );
for (int c=0;c<cards.size();c++) {
Card card = (Card)cards.elementAt(c);
if (!card.getName().equals(Card.WILDCARD)) {
card.setCountry( map.getCountryInt(c+1) );
}
}
fireContentsChanged(this,0,getSize()-1);
cardsList.clearSelection();
}
}
}
public void add() {
Object[] message = new Object[2];
message[0] = new JLabel("Amount of new cards:");
message[1] = new JSpinner( new SpinnerNumberModel( 20,0,500,1) );
int result = openOptionDialog(message,"New Cards");
if (result == JOptionPane.OK_OPTION ) {
int number = ((Integer)((JSpinner)message[1]).getValue()).intValue();
int n = number;
Vector cards = map.getCards();
Vector wilds = new Vector();
for (int c=0;c<cards.size();c++) {
if ( ((Card)cards.elementAt(c)).getName().equals(Card.WILDCARD) ) {
wilds.add( cards.remove(c) );
c--;
}
}
Country[] countries = map.getCountries();
for (int c=cards.size(); c<countries.length && number>0;c++) {
cards.add( new Card(
((c%3)==0)?Card.CAVALRY:
( ((c%3)==1)?Card.INFANTRY:Card.CANNON )
,countries[c]) );
number--;
}
for (int c=0;c<number;c++) {
cards.add( new Card(Card.WILDCARD,null) );
}
cards.addAll(wilds);
fireContentsChanged(this,cards.size()-n, cards.size()-1 );
}
}
}
class MissionsListModel extends AbstractListModel implements ViewTab {
public void changed() {
super.fireContentsChanged(this, 0, map.getNoMissions()-1);
missionsList.clearSelection();
}
public Object getElementAt(int index) {
if (map==null) { return null; }
return map.getMissions().elementAt(index);
}
public int getSize() {
if (map==null) { return 0; }
return map.getNoMissions();
}
private JComboBox makeContinentsJComboBox() {
JComboBox a = new JComboBox( map.getContinents() );
a.insertItemAt(RiskGame.ANY_CONTINENT,0);
a.insertItemAt(null,0);
return a;
}
private Object[] newBox(int a,int b,String text,Player p,Continent c1,Continent c2, Continent c3) {
Object[] message = new Object[14];
message[0] = new JLabel("Player:");
message[1] = new JComboBox( map.getPlayers().toArray() );
((JComboBox)message[1]).insertItemAt(null,0);
message[2] = new JLabel("Number of countries:");
message[3] = new JSpinner( new SpinnerNumberModel(a,0,100,1) );
message[4] = new JLabel("Number of armies:");
message[5] = new JSpinner( new SpinnerNumberModel(b,0,100,1) );
message[6] = new JLabel("Continent 1:");
message[7] = makeContinentsJComboBox();
message[8] = new JLabel("Continent 2:");
message[9] = makeContinentsJComboBox();
message[10] = new JLabel("Continent 3:");
message[11] = makeContinentsJComboBox();
message[12] = new JLabel("Discription:");
message[13] = new OptionPaneTextArea(text);
// set all the values from the current one
((JComboBox)message[1]).setSelectedItem( p );
((JComboBox)message[7]).setSelectedItem( c1 );
((JComboBox)message[9]).setSelectedItem( c2 );
((JComboBox)message[11]).setSelectedItem( c3 );
return message;
}
public void edit() {
int sel = missionsList.getSelectedIndex();
if (sel != -1) {
Mission mission = (Mission)getElementAt(sel);
Object[] message = newBox(
mission.getNoofcountries(),
mission.getNoofarmies(),
mission.getDiscription(),
mission.getPlayer(),
mission.getContinent1(),
mission.getContinent2(),
mission.getContinent3()
);
int result = openOptionDialog(message,"Edit mission");
if (result == JOptionPane.OK_OPTION ) {
mission.setPlayer( (Player) ((JComboBox)message[1]).getSelectedItem() );
mission.setNoofcountries( ((Integer)((JSpinner)message[3]).getValue()).intValue() );
mission.setNoofarmies( ((Integer)((JSpinner)message[5]).getValue()).intValue() );
mission.setContinent1( (Continent) ((JComboBox)message[7]).getSelectedItem() );
mission.setContinent2( (Continent) ((JComboBox)message[9]).getSelectedItem() );
mission.setContinent3( (Continent) ((JComboBox)message[11]).getSelectedItem() );
mission.setDiscription( ((OptionPaneTextArea)message[13]).getLineText() );
fireContentsChanged(this,sel, sel );
}
}
}
public void remove() {
Object[] m = missionsList.getSelectedValues();
if (m.length!=0) {
String missionsString="";
for (int c=0;c<m.length;c++) {
missionsString = missionsString + "\n" + m[c];
}
int result = JOptionPane.showConfirmDialog(MapEditorViews.this, "Are you sure you want to remove:"+
missionsString
, "Remove?", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
Vector missions = map.getMissions();
missions.removeAll( Arrays.asList(m) );
fireContentsChanged(this,0,missions.size()-1);
missionsList.clearSelection();
}
}
}
public void add() {
Object[] message = newBox(0,0,"",null,null,null,null);
int result = openOptionDialog(message,"New mission");
if (result == JOptionPane.OK_OPTION ) {
Mission mission = new Mission(
(Player) ((JComboBox)message[1]).getSelectedItem(),
((Integer)((JSpinner)message[3]).getValue()).intValue(),
((Integer)((JSpinner)message[5]).getValue()).intValue(),
(Continent) ((JComboBox)message[7]).getSelectedItem(),
(Continent) ((JComboBox)message[9]).getSelectedItem(),
(Continent) ((JComboBox)message[11]).getSelectedItem(),
((OptionPaneTextArea)message[13]).getLineText()
);
Vector missions = map.getMissions();
missions.add(mission);
fireContentsChanged(this,missions.size()-1,missions.size()-1);
}
}
}
class OptionPaneTextArea extends JScrollPane {
private JTextArea textarea;
public OptionPaneTextArea(String a) {
super();
textarea = new JTextArea( a , 5, 20 );
textarea.setWrapStyleWord(true);
textarea.setLineWrap(true);
setViewportView(textarea);
}
public String getText() {
return textarea.getText();
}
public String getLineText() {
return getText().replace('\n',' ');
}
public String[] getLines() {
return getText().split("\\n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -