Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored Singlebibtexfield #979

Merged
merged 2 commits into from
Mar 17, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 180 additions & 0 deletions src/main/java/net/sf/jabref/bibtex/BibtexSingleField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package net.sf.jabref.bibtex;

import java.util.EnumSet;
import java.util.Set;

import net.sf.jabref.gui.GUIGlobals;

// --------------------------------------------------------------------------
// a container class for all properties of a bibtex-field
// --------------------------------------------------------------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look at the comments and try to use JavaDoc instead.

public class BibtexSingleField {

// some fieldname constants
public static final double DEFAULT_FIELD_WEIGHT = 1;
public static final double MAX_FIELD_WEIGHT = 2;

public static final double SMALL_W = 0.30;
public static final double MEDIUM_W = 0.5;
public static final double LARGE_W = 1.5;


private enum Flag {
STANDARD,
PRIVATE,
DISPLAYABLE,
WRITEABLE;
}


// the field name
private final String name;

// contains the standard, private, displayable, writable infos
// default is: not standard, public, displayable and writable
private final Set<Flag> flag = EnumSet.of(Flag.DISPLAYABLE, Flag.WRITEABLE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be flags as a set contains more than one flag


private int length = GUIGlobals.DEFAULT_FIELD_LENGTH;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could the default field lenght moved to this class as well?

private double weight = DEFAULT_FIELD_WEIGHT;

// the extras data
// fieldExtras contains mappings to tell the EntryEditor to add a specific
// function to this field, for instance a "browse" button for the "pdf" field.
private Set<BibtexSingleFieldProperties> extras = EnumSet.noneOf(BibtexSingleFieldProperties.class);

// a comma separated list of alternative bibtex-fieldnames, e.g.
// "LCCN" is the same like "lib-congress"
// private String otherNames = null ;


// a Hashmap for a lot of additional "not standard" properties
// todo: add the handling in a key=value manner
// private HashMap props = new HashMap() ;

public BibtexSingleField(String fieldName, boolean pStandard) {
name = fieldName;
setFlag(pStandard, Flag.STANDARD);
}

public BibtexSingleField(String fieldName, boolean pStandard, double pWeight) {
name = fieldName;
setFlag(pStandard, Flag.STANDARD);
weight = pWeight;
}

public BibtexSingleField(String fieldName, boolean pStandard, int pLength) {
name = fieldName;
setFlag(pStandard, Flag.STANDARD);
length = pLength;
}

public BibtexSingleField(String fieldName, boolean pStandard, double pWeight, int pLength) {
name = fieldName;
setFlag(pStandard, Flag.STANDARD);
weight = pWeight;
length = pLength;
}

// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such lines can simply be removed


/**
* Sets or onsets the given flag
* @param setToOn if true, set the flag; if false, unset the flat
* @param flagID, the id of the flag
*/
private void setFlag(boolean setToOn, Flag flagID) {
if (setToOn) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be possible to only set the flags in the constructor and not allow it to be changed later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly. However, the way the current code looks it is a bit of work and since this is primarily used for the special fields, I think it is better to wait until that is reworked.

// set the flag
flag.add(flagID);
} else {
// unset the flag
flag.remove(flagID);
}
}

// -----------------------------------------------------------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove these lines? There are some others in this file similar to this as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public boolean isStandard() {
return flag.contains(Flag.STANDARD);
}

public void setPrivate() {
flag.add(Flag.PRIVATE);
}

public boolean isPrivate() {
return flag.contains(Flag.PRIVATE);
}

public void setDisplayable(boolean value) {
setFlag(value, Flag.DISPLAYABLE);
}

public boolean isDisplayable() {
return flag.contains(Flag.DISPLAYABLE);
}

public void setWriteable(boolean value) {
setFlag(value, Flag.WRITEABLE);
}

public boolean isWriteable() {
return flag.contains(Flag.WRITEABLE);
}

// -----------------------------------------------------------------------

public void setExtras(Set<BibtexSingleFieldProperties> pExtras) {
extras = pExtras;
}

// fieldExtras contains mappings to tell the EntryEditor to add a specific
// function to this field, for instance a "browse" button for the "pdf" field.
public Set<BibtexSingleFieldProperties> getExtras() {
return extras;
}

// -----------------------------------------------------------------------

public void setWeight(double value) {
this.weight = value;
}

public double getWeight() {
return this.weight;
}

/**
* @return The maximum (expected) length of the field value; <em>not</em> the length of the field name
*/
public int getLength() {
return this.length;
}

// -----------------------------------------------------------------------

public String getFieldName() {
return name;
}

/**
* Set this field's numeric property
*
* @param numeric true to indicate that this is a numeric field.
* @return this BibtexSingleField instance. Makes it easier to call this
* method on the fly while initializing without using a local variable.
*/
public BibtexSingleField setNumeric(boolean numeric) {
if (numeric) {
extras.add(BibtexSingleFieldProperties.NUMERIC);
} else {
extras.remove(BibtexSingleFieldProperties.NUMERIC);
}
return this;
}

public boolean isNumeric() {
return extras.contains(BibtexSingleFieldProperties.NUMERIC);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.sf.jabref.bibtex;

import java.util.EnumSet;

public enum BibtexSingleFieldProperties {
YES_NO,
URL,
DATEPICKER,
JOURNAL_NAMES,
EXTERNAL,
BROWSE,
SET_OWNER,
MONTH,
FILE_EDITOR,
NUMERIC;

public static final EnumSet<BibtexSingleFieldProperties> ALL_OPTS = EnumSet
.allOf(BibtexSingleFieldProperties.class);

}
7 changes: 4 additions & 3 deletions src/main/java/net/sf/jabref/gui/FieldWeightDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

import net.sf.jabref.bibtex.BibtexSingleField;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.entry.BibEntry;

Expand Down Expand Up @@ -77,8 +78,8 @@ private JPanel buildMainPanel() {

for (String field : fields) {
builder.append(field);
int weight = (int) ((100 * InternalBibtexFields.getFieldWeight(field)) / GUIGlobals.MAX_FIELD_WEIGHT);
//System.out.println(weight);
int weight = (int) ((100 * InternalBibtexFields.getFieldWeight(field))
/ BibtexSingleField.MAX_FIELD_WEIGHT);
JSlider slider = new JSlider(0, 100, weight);//,);
sliders.put(slider, new SliderInfo(field, weight));
builder.append(slider);
Expand Down Expand Up @@ -120,7 +121,7 @@ private void storeSettings() {
SliderInfo sInfo = sliders.get(slider);
// Only list the value if it has changed:
if (sInfo.originalValue != slider.getValue()) {
double weight = (GUIGlobals.MAX_FIELD_WEIGHT * slider.getValue()) / 100d;
double weight = (BibtexSingleField.MAX_FIELD_WEIGHT * slider.getValue()) / 100d;
InternalBibtexFields.setFieldWeight(sInfo.fieldName, weight);
}
}
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/net/sf/jabref/gui/GUIGlobals.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,9 @@ public class GUIGlobals {
public static Color activeBackground;
public static Color invalidFieldBackgroundColor;

// some fieldname constants
public static final double DEFAULT_FIELD_WEIGHT = 1;
public static final double MAX_FIELD_WEIGHT = 2;

// constants for editor types:
public static final int STANDARD_EDITOR = 1;
public static final int FILE_LIST_EDITOR = 2;

public static final int MAX_BACK_HISTORY_SIZE = 10; // The maximum number of "Back" operations stored.

public static final double SMALL_W = 0.30;
public static final double MEDIUM_W = 0.5;
public static final double LARGE_W = 1.5;

public static final double PE_HEIGHT = 2;

// Size constants for EntryTypeForm; small, medium and large.
Expand Down
Loading