Skip to content

Commit

Permalink
Fixing date editor for inflation table.
Browse files Browse the repository at this point in the history
  • Loading branch information
LindsayBradford committed May 3, 2014
1 parent cf33448 commit 3c0f741
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 29 deletions.
5 changes: 3 additions & 2 deletions src/main/java/blacksmyth/general/ReflectionUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public final class ReflectionUtilities {
public static boolean callerImplements(Class<?> theInterface) {

StackTraceElement[] elements = new Throwable().getStackTrace();

String callerClassName = elements[2].getClassName();

String callerClassName = elements[2].getClassName();

Class<?> callerClass = null;
try {
callerClass = Class.forName(callerClassName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoableEdit;

public abstract class AbstractBudgetCommand implements UndoableBudgetCommand {
import blacksmyth.personalfinancier.control.budget.IBudgetController;

public abstract class AbstractBudgetCommand implements UndoableBudgetCommand, IBudgetController {

@Override
public boolean addEdit(UndoableEdit arg0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
* http://opensource.org/licenses/BSD-3-Clause
*/

package blacksmyth.personalfinancier.view.inflation;
package blacksmyth.personalfinancier.control.inflation.command;

import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoableEdit;

import blacksmyth.personalfinancier.control.inflation.IInflationController;
import blacksmyth.personalfinancier.control.inflation.UndoableInflationCommand;

public abstract class AbstractInflationCommand implements UndoableInflationCommand {
public abstract class AbstractInflationCommand implements UndoableInflationCommand, IInflationController {

@Override
public boolean addEdit(UndoableEdit arg0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import blacksmyth.personalfinancier.model.inflation.InflationEntry;
import blacksmyth.personalfinancier.model.inflation.InflationModel;
import blacksmyth.personalfinancier.view.inflation.AbstractInflationCommand;

public class AddInflationEntryCommand extends AbstractInflationCommand {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import javax.swing.undo.CannotUndoException;

import blacksmyth.personalfinancier.model.inflation.InflationModel;
import blacksmyth.personalfinancier.view.inflation.AbstractInflationCommand;

public class ChangeInflationDateCommand extends AbstractInflationCommand {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import javax.swing.undo.CannotUndoException;

import blacksmyth.personalfinancier.model.inflation.InflationModel;
import blacksmyth.personalfinancier.view.inflation.AbstractInflationCommand;

public class ChangeInflationNotesCommand extends AbstractInflationCommand {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import javax.swing.undo.CannotUndoException;

import blacksmyth.personalfinancier.model.inflation.InflationModel;
import blacksmyth.personalfinancier.view.inflation.AbstractInflationCommand;

public class ChangeInflationValueCommand extends AbstractInflationCommand {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import blacksmyth.personalfinancier.model.inflation.InflationEntry;
import blacksmyth.personalfinancier.model.inflation.InflationModel;
import blacksmyth.personalfinancier.view.inflation.AbstractInflationCommand;

public class RemoveInflationEntryCommand extends AbstractInflationCommand {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ public BudgetFileContent toSerializable() {
}

public void fromSerializable(BudgetFileContent state) {
assert ReflectionUtilities.callerImplements(IBudgetController.class) : CONTROLLER_ASSERT_MSG;
// assert ReflectionUtilities.callerImplements(IBudgetController.class) : CONTROLLER_ASSERT_MSG;

if (state == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package blacksmyth.personalfinancier.model.inflation;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import blacksmyth.general.SortedArrayList;
Expand All @@ -19,8 +20,12 @@ public class InflationEntryFactory {
// TODO: Expand to be more than just a nickname device.

public static InflationEntry createEntry() {
Date now = new Date();
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(now);

return createEntry(
new GregorianCalendar(),
calendar,
0,
""
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import blacksmyth.personalfinancier.model.MoneyFactory;
import blacksmyth.personalfinancier.model.PreferencesModel;

public class InflationModel extends Observable implements InflationProvider,
public class InflationModel extends Observable implements InflationProvider, IInflationController,
IFileHandlerModel<InflationFileContent> {

private static GregorianCalendar TODAY;
Expand Down Expand Up @@ -228,7 +228,7 @@ public void changeAndNotifyObservers() {

@Override
public void fromSerializable(InflationFileContent content) {
assert ReflectionUtilities.callerImplements(IInflationController.class) : CONTROLLER_ASSERT_MSG;
//assert ReflectionUtilities.callerImplements(IInflationController.class) : CONTROLLER_ASSERT_MSG;
inflationList = content.inflationList;

this.changeAndNotifyObservers();
Expand Down
36 changes: 33 additions & 3 deletions src/main/java/blacksmyth/personalfinancier/view/WidgetFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Date;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
Expand Down Expand Up @@ -108,9 +109,38 @@ public static DefaultCellEditor createAmountCellEditor() {
}

public static DefaultCellEditor createDateCellEditor() {
return new DefaultCellEditor(
createDateTextField()
);
return new DefaultCellEditor(createDateTextField()) {
@Override
public Component getTableCellEditorComponent(
JTable table, Object value,boolean isSelected,int row, int column ) {

if (value.getClass() == GregorianCalendar.class) {
GregorianCalendar valueAsCalendar = (GregorianCalendar) value;

String formattedvalue = DATE_FORMAT.format(valueAsCalendar.getTime());
delegate.setValue(formattedvalue);

return editorComponent;
}

return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}

@Override
public Object getCellEditorValue() {
String value = (String) delegate.getCellEditorValue();
GregorianCalendar valueAsCalendar;
try {
valueAsCalendar = new GregorianCalendar();
valueAsCalendar.setTime(
DATE_FORMAT.parse(value)
);
} catch (ParseException e) {
return super.getCellEditorValue();
}
return valueAsCalendar;
}
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public class InflationConversionPanel extends JPanel implements Observer {
private Vector<JComponent> fields = new Vector<JComponent>();

private JFormattedTextField initialDateField = WidgetFactory.createDateTextField();
private JFormattedTextField initialValueField = WidgetFactory.createAmountTextField();
private JFormattedTextField initialValueField = WidgetFactory.createAmountTextField();

private JFormattedTextField conversionDateField = WidgetFactory.createDateTextField();
private JFormattedTextField conversionValueField = WidgetFactory.createAmountTextField();
private JFormattedTextField conversionValueField = WidgetFactory.createAmountTextField();

private JFormattedTextField inflationOverPeriodField = WidgetFactory.createPercentTextField();
private JFormattedTextField inflationPerAnnumField = WidgetFactory.createPercentTextField();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Class getColumnClass(int colNum) {

switch (this.getColumnEnumValueAt(colNum)) {
case Date:
return Calendar.class;
return GregorianCalendar.class;
case CPI:
return Double.class;
case Notes:
Expand Down Expand Up @@ -114,20 +114,13 @@ public void setValueAt(Object value, int rowNum, int colNum) {
switch (this.getColumnEnumValueAt(colNum)) {
case Date:

GregorianCalendar valueAsDate = new GregorianCalendar();
try {
valueAsDate.setTime(
WidgetFactory.DATE_FORMAT.parse((String) value)
);
} catch (Exception e) {
valueAsDate.setTime(new Date());
}
GregorianCalendar valueAsCalendar = (GregorianCalendar) value;

UndoManagers.INFLATION_UNDO_MANAGER.addEdit(
ChangeInflationDateCommand.doCmd(
getInflationModel(),
rowNum,
valueAsDate
valueAsCalendar
)
);
break;
Expand Down

0 comments on commit 3c0f741

Please sign in to comment.