0

I'm using TextStyle and Styler to provide coloring in an SWT window I am creating.

I'm having a problem in my Styler method applyChanges(). The method (an abstract method built into Styler by the authors of SWT) takes a TextStyle object and applies formatting to it, for example, like this:

public void applyStyles(TextStyle textStyle) {
    textStyle.background = new Color(null, new RGB(102, 0, 58));
    //yada yada
}

However, instead of applying styles directly to it, I would like to assign an attribute that already carries all the styles. In other words, I am trying this:

public void applyStyles(TextStyle textStyle) {
    textStyle= this.textStyleAttributeStyle;

}

Unfortunately, though this is not working and I believe it has something to do with passing the attribute by reference because when I copy the attributes one by one, for example like

public void applyStyles(TextStyle textStyle) {
        textStyle.background= this.textStyleAttributeStyle.background;

    }

it works great!

So my question is, how can I provide a complete clone/copy of every attribute in textStyleAttributeStyle and copy it to the corresponding attribute in textStyle in the shortest amount of code?

CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
  • 1
    _passing the attribute by reference_ No. Java is always by value. To achieve the effect of _by reference_, you'll need to pass a object that has a reference to the `textStyle` and change that, just like you do for `Color`. – Sotirios Delimanolis Jun 14 '13 at 16:15
  • Cloning/Copying properties of an object: http://stackoverflow.com/questions/2156120/java-recommended-solution-for-deep-cloning-copying-an-instance – Mubin Jun 14 '13 at 16:17

1 Answers1

0

I think the Java Deep Cloning Library could be usefull for you,

Cloner cloner=new Cloner();

MyClass clone=cloner.deepClone(o);
// clone is a deep-clone of o