Java tutorial
/** * Copyright (C) 2013 Rusty Gerard * * This program 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 3 of the License, or * (at your option) any later version. * * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ package com.callidusrobotics; import java.awt.Color; import java.util.List; import org.apache.commons.lang3.StringUtils; import com.callidusrobotics.attribute.Dimensional; import com.callidusrobotics.locale.MutableCoordinate; import com.callidusrobotics.object.AbstractObject; import com.callidusrobotics.object.actor.PlayerCharacter; import com.callidusrobotics.swing.Console; import com.callidusrobotics.swing.ConsoleFactory; import com.callidusrobotics.swing.ConsoleGraphic; import com.callidusrobotics.ui.ConsoleTextBox; import com.callidusrobotics.ui.MessageBox; import com.callidusrobotics.ui.PaginatedMenuBox; import com.callidusrobotics.ui.PlayerDataBox; import com.callidusrobotics.ui.TextBoxBuilder; import com.callidusrobotics.ui.TextBoxBuilder.Layout; @SuppressWarnings("PMD.TooManyMethods") public class ConsoleColleague { private static final int NUM_DISPLAY_MODES = 2; private final Console console; private final MessageBox msgeBufferBoxTall, msgBufferBoxShort; private PlayerDataBox playerDataBox; private ConsoleTextBox leftPane, rightPane; private final Color boxForeground, boxBackground, fontForeground, fontBackground, selectForeground, selectBackground; private int displayMode = 0; public ConsoleColleague(final Console console, final PlayerCharacter player, final Color boxForeground, final Color boxBackground, final Color fontForeground, final Color fontBackground, final Color selectForeground, final Color selectBackground) { this.console = console; this.boxForeground = boxForeground; this.boxBackground = boxBackground; this.fontForeground = fontForeground; this.fontBackground = fontBackground; this.selectForeground = selectForeground; this.selectBackground = selectBackground; final int displayWidth = getWidth() - getDivColumn() + 1; msgeBufferBoxTall = TextBoxBuilder.buildFancyMessageBox(null, getHeight(), displayWidth, boxForeground, boxBackground, Layout.TOPRIGHT); msgBufferBoxShort = TextBoxBuilder.buildFancyMessageBox(null, getHeight() / 2, displayWidth, boxForeground, boxBackground, Layout.TOPRIGHT); playerDataBox = TextBoxBuilder.buildPlayerDataBox(player, getHeight() / 2, displayWidth, boxForeground, boxBackground, Layout.BOTTOMRIGHT); leftPane = rightPane = null; } public static int getHeight() { return ConsoleFactory.getInstance().getHeight(); } public static int getWidth() { return ConsoleFactory.getInstance().getWidth(); } /** * Divide the console into two panes, a square on the left and the rest on the * right. * * @return The dividing column of the two panes */ public static int getDivColumn() { return Math.min(ConsoleFactory.getInstance().getWidth(), ConsoleFactory.getInstance().getHeight()); } /** * Computes the top-left position of the box when centered in the left pane. * * @param box * The box to center in the console * @return The top left corner */ public static MutableCoordinate getPosition(final Dimensional box) { final int row = ConsoleFactory.getInstance().getHeight() - 1; final int col = getDivColumn(); final int hOffset = (col - box.getWidth()) / 2; final int vOffset = (row - box.getHeight()) / 2; return new MutableCoordinate(vOffset, hOffset); } /** * Computes the top-left position of the box when centered in the whole * console. * * @param box * The box to center in the console * @return The top left corner */ public static MutableCoordinate getPositionCentered(final Dimensional box) { final int row = ConsoleFactory.getInstance().getHeight(); final int col = ConsoleFactory.getInstance().getWidth(); final int hOffset = (col - box.getWidth()) / 2; final int vOffset = (row - box.getHeight()) / 2; return new MutableCoordinate(vOffset, hOffset); } public void toggleDisplayMode() { displayMode = (displayMode + 1) % NUM_DISPLAY_MODES; drawDisplay(); } public void setPlayer(final PlayerCharacter player) { final int displayWidth = getWidth() - getDivColumn() + 1; playerDataBox = TextBoxBuilder.buildPlayerDataBox(player, getHeight() / 2, displayWidth, boxForeground, boxBackground, Layout.BOTTOMRIGHT); drawDisplay(); } public void draw(final AbstractObject object) { object.draw(console); } public int select(final String message, final List<String> list, final boolean trimmed, final boolean preformatted) { PaginatedMenuBox menu; if (trimmed) { int width = message.length(); for (final String listItem : list) { if (listItem.length() > width) { width = listItem.length(); } } if (width % 2 == 0) { width++; } menu = TextBoxBuilder.buildFancyMenu( String.format("%" + (width - (width - message.length()) / 2) + "s", message), list.size() + 7, width + 4, boxForeground, boxBackground, selectForeground, selectBackground, Layout.CENTER); } else { menu = TextBoxBuilder.buildFancyMenu(message, getHeight(), getWidth(), boxForeground, boxBackground, selectForeground, selectBackground, Layout.CENTER); } for (final String listItem : list) { menu.addLine(listItem, fontForeground, fontBackground, preformatted); } console.clear(); menu.draw(console); final int index = menu.selectIndex(console, true); console.clear(); drawDisplay(); return index; } public void drawLeftPane(final ConsoleTextBox box) { draw(leftPane, box); leftPane = box; } public void drawRightPane(final ConsoleTextBox box) { draw(rightPane, box); rightPane = box; } protected void draw(final ConsoleTextBox previous, final ConsoleTextBox next) { // Clear the previously drawn box if (previous != null) { final int row = previous.getRow(); final int col = previous.getCol(); for (int r = row; r < row + previous.getHeight(); r++) { for (int c = col; c < col + previous.getWidth(); c++) { console.print(r, c, ' ', boxBackground, boxBackground); } } } if (next != null) { next.draw(console); } } public void printMessageLogEntry(final String string) { printMessageLogEntry(string, fontForeground, fontBackground); } public void printMessageLogEntry(final String string, final Color foreground, final Color background) { msgeBufferBoxTall.addLine(string, foreground, background, false); msgBufferBoxShort.addLine(string, foreground, background, false); drawDisplay(); } public void print(final String text, final Color foreground, final Color background) { int row = 0, col = 0; for (final char character : text.toCharArray()) { if (character == '\n') { row++; col = 0; } else { console.print(row, col++, character, foreground, background); } } console.render(); console.getKeyPress(); console.clear(); } public void splash(final String text, final boolean trimmed) { MessageBox box; if (trimmed) { final int newlines = StringUtils.countMatches(text, "\n"); box = TextBoxBuilder.buildFancyMessageBox(null, newlines + 5, text.length() + 2 - newlines, boxForeground, boxBackground, Layout.CENTER); } else { box = TextBoxBuilder.buildFancyMessageBox(null, getHeight(), getWidth(), boxForeground, boxBackground, Layout.CENTER); } box.addLine(text, fontForeground, fontBackground, true); console.clear(); box.draw(console); box.pause(console); console.clear(); if (leftPane != null) { leftPane.draw(console); } drawDisplay(); } public void alert(final String message) { // TODO: A height of 10 rows is just a guess. Try and determine the box height based on length of text. final MessageBox box = TextBoxBuilder.buildMessageBox("Press ENTER or ESC to quit.", 10, 40, boxForeground, boxBackground, Layout.CENTER); box.addLine("#", fontBackground, fontBackground, false); final String[] lines = message.split("\n"); for (final String line : lines) { box.addLine(line, fontForeground, fontBackground, false); } console.clear(); box.draw(console); box.pause(console); console.clear(); leftPane.draw(console); drawDisplay(); } public boolean confirm(final String message) { final PaginatedMenuBox menu = TextBoxBuilder.buildMenu(message, 7, 30, boxForeground, boxBackground, selectForeground, selectBackground, Layout.CENTER); menu.addLine("Yes", fontForeground, fontBackground, false); menu.addLine("No", fontForeground, fontBackground, false); console.clear(); menu.draw(console); final String result = menu.selectValue(console, true); console.clear(); leftPane.draw(console); msgeBufferBoxTall.draw(console); return !(result != null && result.equals("Yes")); } public int getQuantity(final int defaultVal, final int maxLen) { final MessageBox box = TextBoxBuilder.buildMessageBox("", 10, 40, boxForeground, boxBackground, Layout.CENTER); final String prompt = "\n\n\n Enter quantity (" + defaultVal + "): "; box.addLine(prompt, fontForeground, fontBackground, true); console.clear(); console.setCursor(box.getRow() + 4, box.getCol() + prompt.length() - 2, new ConsoleGraphic('_', fontForeground, fontBackground)); box.draw(console); final String input = console.getline(maxLen, fontForeground, fontBackground); console.clear(); leftPane.draw(console); drawDisplay(); try { return Integer.parseInt(input); } catch (final NumberFormatException e) { return defaultVal; } } public void setTitle(final String title) { console.setTitle(title); } public void clear() { console.clear(); } public void render() { console.render(); } private void drawDisplay() { if (leftPane != null) { leftPane.draw(console); } if (displayMode == 1) { msgeBufferBoxTall.draw(console); } else { msgBufferBoxShort.draw(console); playerDataBox.draw(console); } } }